美文网首页
Remove Adjacent Repeated Charact

Remove Adjacent Repeated Charact

作者: GakkiLove | 来源:发表于2018-05-24 21:47 被阅读0次

Remove adjacent, repeated characters in a given string, leaving only two characters for each group of such characters. The characters in the string are sorted in ascending order.

Examples

“aaaabbbc” is transferred to “aabbc”

class Solution(object):
  def deDup(self, input):
    if not input or len(input) < 2:
      return input
    lst = list(input)
    left,right = 2,2
    while right < len(lst):
      if lst[right] != lst[left-2]:
        lst[left] = lst[right]
        left += 1
      right += 1
    return ''.join(lst[:left])

相关文章

网友评论

      本文标题:Remove Adjacent Repeated Charact

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