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 <= 2001 <= T.length <= 200S` and `T` only contain lowercase letters and `'#'` characters.

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

Solution:

``````func backspaceCompare(S string, T string) bool {
lenS, lenT:= len(S), len(T)
s, t := lenS - 1, lenT -1;
delS, delT := 0, 0;

for s >= 0 || t >= 0 {
for s >= 0 {
if S[s] == '#' {
s--
delS++
} else if delS > 0 {
delS--;
s--
} else {
break;
}
}

for t >= 0 {
if T[t] == '#' {
t--
delT++
} else if delT > 0 {
delT--;
t--
} else {
break;
}
}
//((s >= 0) != (t >= 0)) important

if (s >= 0 && t >= 0 && S[s] != T[t]) || ((s >= 0) != (t >= 0))  {
return false
}
s--;
t--;
}

return  true
}``````