1047. Remove All Adjacent Duplicates In String
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/
Python
class Solution:
def removeDuplicates(self, s: str) -> str:
stack = []
for letter in s:
if stack and letter == stack[-1]:
stack.pop()
else:
stack.append(letter)
return ''.join(stack)
Go
func removeDuplicates(s string) string {
var stack []rune
for _, letter := range s {
if len(stack) > 0 && letter == stack[len(stack)-1] {
stack = stack[:len(stack)-1]
} else {
stack = append(stack, letter)
}
}
return string(stack)
}