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])
网友评论