forked from varunu28/LeetCode-Java-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackspace String Compare.java
More file actions
45 lines (45 loc) · 853 Bytes
/
Backspace String Compare.java
File metadata and controls
45 lines (45 loc) · 853 Bytes
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
42
43
44
45
class Solution {
public boolean backspaceCompare(String S, String T) {
int i = S.length() - 1;
int j = T.length() - 1;
int skipS = 0;
int skipT = 0;
while (i >= 0 || j >= 0) {
while (i >= 0) {
if (S.charAt(i) == '#') {
skipS++;
i--;
}
else if (skipS > 0) {
skipS--;
i--;
}
else {
break;
}
}
while (j >= 0) {
if (T.charAt(j) == '#') {
skipT++;
j--;
}
else if (skipT > 0) {
skipT--;
j--;
}
else {
break;
}
}
if (i >= 0 && j >= 0 && S.charAt(i) != T.charAt(j)) {
return false;
}
if ((i >= 0) != (j >= 0)) {
return false;
}
i--;
j--;
}
return true;
}
}