FastPrepRepeated Adjacent Pair Deletion

Repeated Adjacent Pair Deletion

Google logoGoogle● EasyNEW GRADONSITE INTERVIEW
Learn

Problem statement

Given a string s, repeatedly remove adjacent pairs of equal characters. After a removal, newly adjacent equal characters must also be removed.

Return the unique stable string that remains. The process is equivalent to applying removals from left to right with a stack.

Function

removeAdjacentPairs(s: String) → String

Examples

Example 1

s = "ACCAB"return = "B"

Remove CC to get AAB, then remove AA to get B.

Example 2

s = "abbaca"return = "ca"

Removing bb creates aa, whose removal leaves ca.

Example 3

s = "abc"return = "abc"

No adjacent pair is equal.

Constraints

  • 0 <= s.length <= 200000.
  • s contains ASCII letters.

More Google problems

See Google hiring insights
public String removeAdjacentPairs(String s) {
    // Write your code here.
}
s"ACCAB"
expected"B"
Checking account…