https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/
重複兩次的字移除,直到沒有重複的字
abbaca ⇒ ca
Example 1:
Input: s = "abbaca"
Output: "ca"
Explanation:
For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move. The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca".
Example 2:
Input: s = "azxxzy"
Output: "ay"
兩種解法:stack與two pointer
stack = []
for i in s
return stack
func removeDuplicates(s string) string {
stack := []rune{}
for _, b := range s {
if len(stack) > 0 && stack[len(stack)-1] == b {
stack = stack[:len(stack)-1]
continue
}
stack = append(stack, b)
}
return string(stack)
}