-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBackspace String Compare
50 lines (48 loc) · 1.29 KB
/
Backspace String Compare
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
47
48
49
50
class Solution {
public boolean backspaceCompare(String S, String T) {
ArrayList<Character> s = new ArrayList<>();
ArrayList<Character> t = new ArrayList<>();
int count = 0;
for(int i = 0; i<S.length(); i++){
if(S.charAt(i) != '#'){
count++;
s.add(S.charAt(i));
}
else{
if(count >= 1){
count--;
s.remove(count);
//count--;
}
}
//System.out.println(s);
}
count = 0;
for(int i = 0; i<T.length(); i++){
if(T.charAt(i) != '#'){
count++;
t.add(T.charAt(i));
}
else{
if(count >= 1){
count--;
t.remove(count);
//count--;
}
}
//System.out.println(t);
}
if(s.size() != t.size()){
return false;
}
if(s.size() == t.size() && s.size() == 0){
return true;
}
for(int i = 0; i<s.size(); i++){
if(s.get(i) != t.get(i)){
return false;
}
}
return true;
}
}