-
Notifications
You must be signed in to change notification settings - Fork 43
/
BackspaceStringCompare.java
121 lines (94 loc) · 2.8 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package LeetCodeJava.TwoPointer;
// https://leetcode.com/problems/backspace-string-compare/
import java.util.Stack;
public class BackspaceStringCompare {
// V0
// IDEA : STACK + stringBuilder
public boolean backspaceCompare(String s, String t) {
if (s == null && t == null){
return true;
}
if (s == null || t == null){
return false;
}
String s_ = backSpaceStr(s);
String t_ = backSpaceStr(t);
return s_.equals(t_);
}
private String backSpaceStr(String input){
Stack<String> stack = new Stack<>();
for(String x: input.split("")){
if (!x.equals("#")){
stack.add(x);
}else{
if(!stack.isEmpty()){
stack.pop();
}
}
}
// stack to str
StringBuilder sb = new StringBuilder();
while(!stack.isEmpty()){
sb.append(stack.pop());
}
return sb.toString();
}
// V1
public boolean backspaceCompare_1(String s, String t) {
if (s == null && t == null){
return true;
}
// TODO : check why can't have this condition
// if ( (s == null || t != null) || (s != null && t == null) ){
// return false;
// }
String _s = backSpaceOp(s);
String _t = backSpaceOp(t);
//
// System.out.println("_s = " + _s);
// System.out.println("_t = " + _t);
if (_s == null && _t == null){
return true;
}
return _s.equals(_t);
}
private static String backSpaceOp(String s){
if (s == null){
return "";
}
Stack<String> stack = new Stack<>();
for (int i = 0; i < s.length(); i++){
String cur = String.valueOf(s.charAt(i));
System.out.println("cur = " + cur + ", stack = " + stack.toString() + " is # ?" + cur.equals("#"));
if (!cur.equals("#")){
stack.push(cur);
}else{
if (!stack.empty()){
stack.pop();
}
}
}
String res = "";
while (!stack.isEmpty()){
String _cur = stack.pop();
res += _cur;
}
return res;
}
// V2
// IDEA : BUILD STRING
// https://leetcode.com/problems/backspace-string-compare/editorial/
public boolean backspaceCompare_2(String S, String T) {
return build(S).equals(build(T));
}
public String build(String S) {
Stack<Character> ans = new Stack();
for (char c: S.toCharArray()) {
if (c != '#')
ans.push(c);
else if (!ans.empty())
ans.pop();
}
return String.valueOf(ans);
}
}