We have two strings where the # character acts like a backspace key. When you encounter a #, it deletes the character immediately before it (if one exists). If the text is already empty, the backspace does nothing.
Our job is to figure out whether both strings produce the same final result after processing all the backspaces.
The direct solution simulates a text editor: process each string character by character, apply backspaces as you go, then compare the two results. The follow-up asks for O(1) space, which rules out building the final strings and points to a two-pointer scan that works backwards through both inputs.
1 <= s.length, t.length <= 200 → Any linear solution runs fast at this size. The interesting constraint is the follow-up: O(n) time and O(1) space.s and t only contain lowercase letters and '#' characters → Every character is either a letter to type or a backspace, so there are no other cases to handle.Simulate what a text editor does. Read characters from left to right: a regular letter gets typed, and a # deletes the last character typed (if any).
A stack matches this behavior directly: push letters, pop on #. After processing both strings, whatever remains in each stack is the final text. If the two results match, return true.
# and the stack isn't empty, pop the top element.s and t.The trace below types out both strings; each stack holds the editor's text so far.
The result is correct, but each string needs a separate stack that grows and shrinks as characters arrive. The next approach runs the same simulation inside a single buffer.
The text in the editor is always a prefix of the characters kept so far, so the stack can be replaced by two indices over a single buffer: read scans the original characters, and write marks the length of the text typed so far. A letter is copied to position write, which then advances. A # moves write back one slot (if it is above zero), and the next letter overwrites the deleted character.
After the scan, the first write slots of the buffer hold the final text. Run this on both strings, then compare the two lengths and prefixes.
When strings are immutable, the buffer is a copy of the input, so this version removes the push/pop container churn rather than asymptotic space. Where the input string's own storage can be modified, no copy is needed and the forward simulation runs in O(1) extra space.
write = 0. For each index read from left to right:buffer[read] is #: decrement write if it is above zero.buffer[read] into buffer[write] and increment write.write characters of the buffer.The trace below runs the read and write indices over s = "ab#c" and t = "ad#c". After the backspace, the next letter overwrites the deleted slot.
Both forward passes keep the typed-out text around until the final comparison. Meeting the O(1) space bound in every language means comparing characters without storing any text, which requires scanning from the other end.
A # only ever deletes characters to its left. Scanning right to left therefore meets each # before the characters it deletes, so a counter of pending backspaces is enough: increment it on #, and consume one count to skip the next letter. Scanning forward does not have this property, because a letter's survival depends on # characters that appear after it. Scanning backward, every character's fate is decided the moment the pointer reaches it.
Start one pointer at the end of each string. For each string, walk left to its next surviving character. Compare the two survivors; if they differ, return false. If one string runs out of surviving characters before the other, the final texts have different lengths, so return false. If both run out together, return true.
i at the end of s and pointer j at the end of t.skipS = 0 and skipT = 0 to count pending backspaces.i >= 0 or j >= 0:i leftward, processing backspaces for s:s[i] is #, increment skipS and move left.skipS > 0, decrement skipS (skip this character) and move left.i is at a valid character. Stop.j with string t.i >= 0 and j >= 0: compare s[i] and t[j]. If they differ, return false.i and j to move to the next characters.The earlier examples never stack more than one pending backspace, so this trace uses s = "a##c" and t = "#a#c". Both reduce to "c", and t starts with a backspace on empty text.