backspace-string-compare
Problem
Problem Description
Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character.
Example 1:
Input: S = "ab#c", T = "ad#c"
Output: true
Explanation: Both S and T become "ac".
Example 2:
Input: S = "ab##", T = "c#d#"
Output: true
Explanation: Both S and T become "".
Example 3:
Input: S = "a##c", T = "#a#c"
Output: true
Explanation: Both S and T become "c".
Example 4:
Input: S = "a#c", T = "b"
Output: false
Explanation: S becomes "c" while T becomes "b".
Note:
1 <= S.length <= 200
1 <= T.length <= 200
S and T only contain lowercase letters and '#' characters.
Follow up:
Can you solve it in O(N) time and O(1) space?Solution 1
In this solution, we build a new string after backspace.
From left to right, if curr char != '#', append to newString.
If curr char == '#', check newString is null?
if newString is null, do nothing, continue
if newString is not null, delete last char.
return newString.
Compare new build string from S and T.
For example:

Complexity Analysis
Time Complexity: O(N + M)
Space Complexity: O(N + M)
N - length of String S
M - length of String T
Code
Solution 2 (follow up)
This solution is to compare char on the fly.
Scan both String S and T from right to left
Define '#' count and index: sIdx, tIdx, if encounter '#', count++, else count--.
Until count = 0, and curr char != '#', check current position char
if out of string(S/T) index, then
s = *, t = *else
s = S.charAt(sIdx), t = T.charAt(tIdx)
compare current
s == t?if
s != t, return falseelse continue
If complete loop S and T, return true.
for example:

Complexity Analysis
Time Complexity: O(N + M)
Space Complexity: O(1)
N - length of String S
M - length of String T
Last updated
Was this helpful?