Skip to content

Latest commit

 

History

History
36 lines (27 loc) · 804 Bytes

remove-all-adjacent-duplicates-in-string.md

File metadata and controls

36 lines (27 loc) · 804 Bytes

Code

func removeDuplicates(s string) string {
	stack := []rune{}

	for _, c := range s {
		if len(stack) == 0 {
			stack = append(stack, c)
		} else {
			if c == stack[len(stack)-1] {
				stack = stack[:len(stack)-1]
			} else {
				stack = append(stack, c)
			}
		}
	}

	return string(stack)
}

Solution in mind

  • We iterate through the given string, character by character while maintaining a stack.

  • A character is pushed onto the stack only if:

    • The stack is empty
    • The character on top of the stack is not the same as the current character
  • If the character on top is the same as the current character, we pop it from the stack as it is a duplicate.

  • After iterating through the string, the stack now has the required new string, character by character.