Backspace String Compare

Practice this coding question on Coding Gurukul.

Problem statement

Two offline terminals record typing sessions as keystroke logs. A lowercase letter appends to the current text, while # represents a backspace. The synchronization service needs to decide whether the users ended with the same text, even when their keystrokes were different. Your task Start each terminal with empty text and process its log from left to right. A # removes the most recently retained character when one exists; on empty text it does nothing. Compare the two final texts and print true if they are equal, otherwise false. Backspaces are commands and never appear in the final text. Both final texts may be empty. Repeated backspaces can remove several earlier letters, and an early extra backspace does not cancel a letter typed later. A stack-based simulation takes linear time; a backward scan can also avoid storing the edited strings. Worked example For cat##r and czz##r, the first terminal removes t and a before adding r. The second removes both z characters before adding r. Both end with cr, so print true.

Input format

Two lines: the first log s, then the second log t.

Output format

Print lowercase true or false.

Constraints

1 <= length of each log <= 200; logs contain lowercase English letters and # only.

Sample input

cat##r
czz##r

Sample output

true

Sign in to run or submit a solution in C++, Java or Python.

Loading Coding Gurukul…