-
Notifications
You must be signed in to change notification settings - Fork 46
/
_844.java
35 lines (28 loc) · 918 Bytes
/
_844.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
/**
* LeetCode 844 - Backspace String Compare
*/
public class _844 {
public boolean backspaceCompare(String S, String T) {
return type(S).equals(type(T));
}
String type(String s) {
StringBuffer buffer = new StringBuffer();
for (char ch : s.toCharArray()) {
if (ch == '#') {
if (buffer.length() > 0) {
buffer.deleteCharAt(buffer.length() - 1);
}
} else {
buffer.append(ch);
}
}
return buffer.toString();
}
public static void main(String[] args) {
_844 sol = new _844();
System.out.println(sol.backspaceCompare("ab#c", "ad#c"));
System.out.println(sol.backspaceCompare("ab##", "c#d#"));
System.out.println(sol.backspaceCompare("a##c", "#a#c"));
System.out.println(sol.backspaceCompare("a#c", "b"));
}
}