-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path846.hand-of-straights.java
46 lines (40 loc) · 1.23 KB
/
846.hand-of-straights.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
42
43
44
45
46
class Solution {
public boolean backspaceCompare(String S, String T) {
int p = T.length()-1;
int i = S.length()-1;
int s_h = 0;
int t_h = 0;
boolean res = true;
while(p>=0 || i >=0){
while(i>=0 ){
if(S.charAt(i) == '#'){
s_h++;
i--;
}else if(s_h > 0){
s_h--;
i--;
}
else break;
//System.out.println(i+" 1 "+s_h);
}
while(p>=0 ){
if(T.charAt(p) == '#'){
t_h++;
p--;
}else if(t_h > 0){
t_h--;
p--;
}
else break;
//System.out.println(i+" 1 "+s_h);
}
if(i>=0 && p>=0 && S.charAt(i) != T.charAt(p))
return false;
if((i>=0) != (p>=0))
return false;
i--;
p--;
}
return res;
}
}