HTMLify

LeetCode - Remove All Adjacent Duplicates In String - Go
Views: 17 | Author: abh
func removeDuplicates(s string) string {
    var stack []rune
    for _, c := range s {
        if len(stack) > 0 && stack[len(stack)-1] == c {
            stack = stack[:len(stack)-1]
        } else {
            stack = append(stack, c)
        }
    }
    var ans string
    for ;len(stack)>0; {
        c, stac := stack[len(stack)-1], stack[:len(stack)-1]
        stack = stac
        ans = string(c) + ans
    }
    return ans
}

Comments