-
Notifications
You must be signed in to change notification settings - Fork 277
/
BackspaceStringCompare.java
79 lines (70 loc) · 1.86 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Author: Shobhit Behl (LC: shobhitbruh)
class Solution {
public boolean backspaceCompare(String s, String t) {
Stack<Character> st1=new Stack<>();
Stack<Character> st2=new Stack<>();
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='#'){
if(st1.size()>0){
st1.pop();
}
}else{
st1.push(s.charAt(i));
}
}
for(int i=0;i<t.length();i++){
if(t.charAt(i)=='#'){
if(st2.size()>0){
st2.pop();
}
}else{
st2.push(t.charAt(i));
}
}
return st1.equals(st2);
}
}
// @saorav21994
// TC : O(n)
// SC : O(1)
// Follow up solution with O(1) space
class Solution {
public boolean backspaceCompare(String s, String t) {
int sl = s.length();
int tl = t.length();
StringBuffer sb = new StringBuffer("");
int back = 0;
for (int i = sl-1; i >= 0; i--) {
if (s.charAt(i) == '#')
back += 1;
else {
if (back > 0) {
back -= 1;
continue;
}
sb.insert(0, s.charAt(i));
}
}
back = 0;
int j = sb.length();
for (int i = tl-1; i >= 0; i--) {
if (t.charAt(i) == '#') {
back += 1;
}
else {
if (back > 0) {
back -= 1;
continue;
}
j -= 1;
if (j >= 0) {
if (t.charAt(i) != sb.charAt(j))
return false;
}
}
}
if (j != 0)
return false;
return true;
}
}