FastPrepRemove Adjacent Duplicate Pairs

Remove Adjacent Duplicate Pairs

Bloomberg LP logoBloomberg LP● EasyNEW GRADPHONE SCREEN
Learn

Problem statement

Given a string text, repeatedly remove any pair of equal adjacent characters until no such pair remains.

Removing one pair may make a new equal pair adjacent. Return the final string after all possible removals. The final result is unique regardless of the order in which valid pairs are removed.

Function

removeAdjacentDuplicatePairs(text: String) → String

Examples

Example 1

text = "abbaca"return = "ca"

Remove bb to get aaca, then remove aa to get ca.

Example 2

text = "azxxzy"return = "ay"

Removing xx exposes zz; removing that pair leaves ay.

Example 3

text = "abc"return = "abc"

No adjacent characters are equal, so the string is unchanged.

Constraints

  • 0 <= text.length <= 100000
  • text contains only lowercase English letters.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public String removeAdjacentDuplicatePairs(String text) {
    // Write your code here.
}
text"abbaca"
expected"ca"
Checking account…