Skip to content
Onsite

Remove All Adjacent Duplicates in String

EasyP2Coding

You are given a string s of lowercase letters. Whenever two equal letters are next to each other, both are removed. Keep doing this until no two neighbours are equal.

Return the remaining string. If nothing remains, return the string "-1".

Example 1

Input
s = "abbaca"
Output
"ca"
Why
Remove "bb" to get "aaca", then "aa" to get "ca".

Example 2

Input
s = "abba"
Output
"-1"

Example 3

Input
s = "abc"
Output
"abc"

Constraints

  • 1 <= s.length <= 10^5
  • s consists of lowercase English letters

Hints

Run checks the 3 visible tests, Submit all 11.