1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/删除字符串两端相同字符后的最短长度 [minimum-length-of-string-after-deleting-similar-ends].html
2022-03-29 12:43:11 +08:00

52 lines
1.9 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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>'a'</code><code>'b'</code> 和 <code>'c'</code> 的字符串 <code>s</code> 你可以执行下面这个操作5 个步骤)任意次:</p>
<ol>
<li>选择字符串 <code>s</code> 一个 <strong>非空</strong> 的前缀,这个前缀的所有字符都相同。</li>
<li>选择字符串 <code>s</code> 一个 <strong>非空</strong> 的后缀,这个后缀的所有字符都相同。</li>
<li>前缀和后缀在字符串中任意位置都不能有交集。</li>
<li>前缀和后缀包含的所有字符都要相同。</li>
<li>同时删除前缀和后缀。</li>
</ol>
<p>请你返回对字符串 <code>s</code> 执行上面操作任意次以后(可能 0 次),能得到的 <strong>最短长度</strong> 。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<b>输入:</b>s = "ca"
<b>输出:</b>2
<strong>解释:</strong>你没法删除任何一个字符,所以字符串长度仍然保持不变。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<b>输入:</b>s = "cabaabac"
<b>输出:</b>0
<b>解释:</b>最优操作序列为:
- 选择前缀 "c" 和后缀 "c" 并删除它们,得到 s = "abaaba" 。
- 选择前缀 "a" 和后缀 "a" 并删除它们,得到 s = "baab" 。
- 选择前缀 "b" 和后缀 "b" 并删除它们,得到 s = "aa" 。
- 选择前缀 "a" 和后缀 "a" 并删除它们,得到 s = "" 。</pre>
<p><strong>示例 3</strong></p>
<pre>
<b>输入:</b>s = "aabccabba"
<b>输出:</b>3
<b>解释:</b>最优操作序列为:
- 选择前缀 "aa" 和后缀 "a" 并删除它们,得到 s = "bccabb" 。
- 选择前缀 "b" 和后缀 "bb" 并删除它们,得到 s = "cca" 。
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> 只包含字符 <code>'a'</code><code>'b'</code> 和 <code>'c'</code> 。</li>
</ul>