leetcode在力扣 App 中打开
调试中...
调试中...
题目描述
题目描述
题解
题解
提交记录
提交记录
代码
代码
测试用例
测试用例
测试结果
测试结果
中等
相关标签
相关企业
提示

给你一个字符串 s 。

你需要对 s 执行以下操作 任意 次:

  • 选择一个下标 i ,满足 s[i] 左边和右边都 至少 有一个字符与它相同。
  • 删除 i 左边 离它 最近 的 s[i] 字符。
  • 删除 i 右边 离它 最近 的 s[i] 字符。

请你返回执行完所有操作后, s 的 最短 长度。

 

示例 1:

输入:s = "abaacbcbb"

输出:5

解释:
我们执行以下操作:

  • 选择下标 2 ,然后删除下标 0 和 3 处的字符,得到 s = "bacbcbb" 。
  • 选择下标 3 ,然后删除下标 0 和 5 处的字符,得到 s = "acbcb" 。

示例 2:

输入:s = "aa"

输出:2

解释:
无法对字符串进行任何操作,所以返回初始字符串的长度。

 

提示:

  • 1 <= s.length <= 2 * 105
  • s 只包含小写英文字母。
通过次数
4.2K
提交次数
8.4K
通过率
50.2%


相关企业

提示 1
Only the frequency of each character matters in finding the final answer.

提示 2
If a character occurs less than 3 times, we cannot perform any process with it.

提示 3
Suppose there is a character that occurs at least 3 times in the string, we can repeatedly delete two of these characters until there are at most 2 occurrences left of it.

评论 (0)

贡献者
© 2025 领扣网络(上海)有限公司
0 人在线
行 1,列 1
s =
"abaacbcbb"
Source