forked from codemistic/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackspaceStringCompare.java
41 lines (37 loc) · 1.24 KB
/
BackspaceStringCompare.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* 844. Backspace String Compare - (https://leetcode.com/problems/backspace-string-compare/)
Given two strings s and t, return true if they are equal when both are typed into empty text editors.
'#' means a backspace character.
Note that after backspacing an empty text, the text will continue empty.
* -------Two pointer approach-------
*/
public class BackspaceStringCompare {
public boolean backspaceCompare(String S, String T) {
int i = S.length() - 1;
int j = T.length() - 1;
int countS = 0;
int countT = 0;
while (i >= 0 || j >= 0) {
while (i >= 0 && (countS > 0 || S.charAt(i) == '#')) {
if (S.charAt(i) == '#')
countS++;
else
countS--;
i--;
}
char left = i < 0 ? '@' : S.charAt(i);
while (j >= 0 && (countT > 0 || T.charAt(j) == '#')) {
if (T.charAt(j) == '#')
countT++;
else
countT--;
j--;
}
char right = j < 0 ? '@' : T.charAt(j);
if (left != right)
return false;
i--;
j--;
}
return true;
}
}