AlgoInsight

DSA problem

Backspace String Compare

EasyTwo PointersPattern: Two Pointers (reverse scan)top600AmazonGoogleFacebookMicrosoft

Given two strings s and t, return true if they are equal when both are typed into empty text editors. The character # means a backspace character.

Note that after backspacing an empty text, the text will continue empty.

Follow-up: Can you solve it in O(n) time and O(1) space?

Examples

Example 1
Input: s = "ab#c", t = "ad#c"
Output: true
Explanation: Both s and t become "ac". In s the b is deleted by #, in t the d is deleted by #.
Example 2
Input: s = "ab##", t = "c#d#"
Output: true
Explanation: Both s and t become "" (the empty string).
Example 3
Input: s = "a#c", t = "b"
Output: false
Explanation: s becomes "c" while t becomes "b", so they are not equal.

Constraints

3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.

two-pointersstringstacksimulation

Browse all DSA problems