1047. Remove All Adjacent Duplicates In String

class Solution:
    def removeDuplicates(self, S: str) -> str:
        i = 0
        while S and i < len(S)-1:
            if S[i] == S[i+1]:
                S = S[:i]+S[i+2:]
                if i > 0: i -= 1
            else:
                i+=1
        return S

Last updated

Was this helpful?