美文网首页
Remove Adjacent Repeated Charact

Remove Adjacent Repeated Charact

作者: GakkiLove | 来源:发表于2018-05-15 20:27 被阅读0次

    Repeatedly remove all adjacent, repeated characters in a given string from left to right.

    No adjacent characters should be identified in the final string.

    Examples

    "abbbaaccz" → "aaaccz" → "ccz" → "z"
    "aabccdc" → "bccdc" → "bdc"

    class Solution(object):
      def deDup(self, input):
        if not input or len(input) < 2:
          return input
        array = list(input)
        i = 1
        end = 0
        while i < len(array):
          if end == -1 or array[i] != array[end]:
            end += 1
            array[end] = array[i]
          else:
            end -= 1
            while i + 1 < len(array) and array[i] == array[i+1]:
              i += 1
          i += 1
        return ''.join(array[:end + 1])
    

    相关文章

      网友评论

          本文标题:Remove Adjacent Repeated Charact

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