Trains the technique from
LeetCode 844Backspace String CompareThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
Two field radios each punch a row of characters onto its own paper tape. A lowercase letter is punched as itself. The character # is a strike mark, and it cancels the nearest letter to its left that no earlier strike mark has already cancelled. A strike mark with no letter left to cancel does nothing at all, and it is never an error.
The reading of a tape is the row of letters that survive, left to right, once every strike mark has been honoured.
Given tapes s and t, return true when the two tapes have the same reading and false otherwise.
Do it in O(1) extra space: work from the cursors you keep, without assembling either reading.
Example 1
Tape `s` loses its `d`, so it reads `gols`. Tape `t` loses its first `l`, then punches `l` and `s`, so it also reads `gols`.
Example 2
Tape `s` reads `not` and tape `t` reads `noe`, and those two readings differ.
Example 3
The two strike marks on `s` cancel `q` and then `p`, so `s` reads as nothing. Each strike mark on `t` cancels the letter before it, so `t` also reads as nothing.
Example 4
The three leading strike marks on `s` have no letters to their left, so they do nothing, and both tapes read `hive`.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def backspace_compare(s: str, t: str) -> bool:public boolean backspaceCompare(String s, String t)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.