Problem · Array
Compare Character Arrays with Backspaces
Learn this problemProblem statement
You are given two arrays of string tokens, first and second. Each token is either a one-character string or the special token -b.
Process each array from left to right. A one-character token appends that character to the current text. The token -b removes the most recently appended character, if one exists. Return true if both arrays produce the same final text; otherwise, return false.
Function
areEqualAfterBackspaces(first: String[], second: String[]) → booleanExamples
Example 1
first = ["a", "b", "-b", "c"]second = ["a", "c"]return = trueThe first array produces ac: append a, append b, remove b, then append c. The second array also produces ac.
Example 2
first = ["-b", "a", "b"]second = ["a", "c"]return = falseThe leading backspace has no effect, so the arrays produce ab and ac.