Skip to content

Commit a499fe9

Browse files
authored
BackspaceStringCompare
P.S bad with java
1 parent 8752ba4 commit a499fe9

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'''
2+
Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character.
3+
'''
4+
class Solution {
5+
public boolean backspaceCompare(String S, String T) {
6+
if (S == null || T == null) return false;
7+
8+
return type(S).equals(type(T));
9+
}
10+
11+
private String type(String s) {
12+
Stack<Character> st = new Stack<>();
13+
for (char c : s.toCharArray()) {
14+
if (c == '#') {
15+
if (!st.isEmpty()) st.pop();
16+
} else {
17+
st.push(c);
18+
}
19+
}
20+
StringBuffer strb = new StringBuffer();
21+
while (!stack.isEmpty()) {
22+
strb.append(st.pop());
23+
}
24+
return strb.toString();
25+
}
26+
}

0 commit comments

Comments
 (0)