美文网首页
LeetCode每日一题:双指针

LeetCode每日一题:双指针

作者: ShowMeCoding | 来源:发表于2022-12-27 21:35 被阅读0次

1750. 删除字符串两端相同字符后的最短长度

输入:s = "cabaabac"
输出:0
解释:最优操作序列为:

  • 选择前缀 "c" 和后缀 "c" 并删除它们,得到 s = "abaaba" 。
  • 选择前缀 "a" 和后缀 "a" 并删除它们,得到 s = "baab" 。
  • 选择前缀 "b" 和后缀 "b" 并删除它们,得到 s = "aa" 。
  • 选择前缀 "a" 和后缀 "a" 并删除它们,得到 s = "" 。
class Solution:
    def minimumLength(self, s: str) -> int:
        left, right = 0, len(s) - 1
        while left < right and s[left] == s[right]:
            c = s[left]
            while left <= right and s[left] == c:
                left += 1
            while right >= left and s[right] == c:
                right -= 1
        return right - left + 1

笔记:使用双指针进行统计计数

相关文章

网友评论

      本文标题:LeetCode每日一题:双指针

      本文链接:https://www.haomeiwen.com/subject/fqjaqdtx.html