1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-09-12 10:51:42 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
Files
leetcode-problemset/leetcode-cn/problem (Chinese)/至多 K 次操作后的最长回文子序列 [longest-palindromic-subsequence-after-at-most-k-operations].html
2025-03-14 03:44:12 +08:00

57 lines
2.4 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给你一个字符串 <code>s</code> 和一个整数 <code>k</code></p>
<p>在一次操作中,你可以将任意位置的字符替换为字母表中相邻的字符(字母表是循环的,因此&nbsp;<code>'z'</code>&nbsp;的下一个字母是&nbsp;<code>'a'</code>)。例如,将 <code>'a'</code> 替换为下一个字母结果是 <code>'b'</code>,将 <code>'a'</code> 替换为上一个字母结果是 <code>'z'</code>;同样,将 <code>'z'</code> 替换为下一个字母结果是 <code>'a'</code>,替换为上一个字母结果是 <code>'y'</code></p>
<p>返回在进行&nbsp;<strong>最多</strong> <code>k</code> 次操作后,<code>s</code>&nbsp;<strong>最长回文子序列&nbsp;</strong>的长度。</p>
<p><strong>子序列&nbsp;</strong>是一个&nbsp;<strong>非空&nbsp;</strong>字符串,可以通过删除原字符串中的某些字符(或不删除任何字符)并保持剩余字符的相对顺序得到。</p>
<p><strong>回文&nbsp;</strong>是正着读和反着读都相同的字符串。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">s = "abced", k = 2</span></p>
<p><strong>输出:</strong> <span class="example-io">3</span></p>
<p><strong>解释:</strong></p>
<ul>
<li><code>s[1]</code> 替换为下一个字母,得到 <code>"acced"</code></li>
<li><code>s[4]</code> 替换为上一个字母,得到 <code>"accec"</code></li>
</ul>
<p>子序列 <code>"ccc"</code> 形成一个长度为 3 的回文,这是最长的回文子序列。</p>
</div>
<p><strong class="example">示例 2</strong></p>
<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">s = "aaazzz", k = 4</span></p>
<p><strong>输出:</strong> 6</p>
<p><strong>解释:</strong></p>
<ul>
<li><code>s[0]</code> 替换为上一个字母,得到 <code>"zaazzz"</code></li>
<li><code>s[4]</code> 替换为下一个字母,得到 <code>"zaazaz"</code></li>
<li><code>s[3]</code> 替换为下一个字母,得到 <code>"zaaaaz"</code></li>
</ul>
<p>整个字符串形成一个长度为 6 的回文。</p>
</div>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 200</code></li>
<li><code>1 &lt;= k &lt;= 200</code></li>
<li><code>s</code> 仅由小写英文字母组成。</li>
</ul>