-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack844.java
More file actions
37 lines (34 loc) · 1.01 KB
/
Stack844.java
File metadata and controls
37 lines (34 loc) · 1.01 KB
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
package stack;
import java.util.Stack;
public class Stack844 {
public static void main(String[] args) {
System.out.println(backspaceCompare("y#fo##f", "y#f#o##f"));
}
public static boolean backspaceCompare(String S, String T) {
Stack stackS = getStack(S), stackT = getStack(T);
if (stackS.size() != stackT.size()) {
return false;
}
while (!stackS.isEmpty()) {
if (stackS.pop() != stackT.pop()) {
return false;
}
}
return true;
}
public static Stack<Character> getStack(String s) {
char[] chars = s.toCharArray();
Stack<Character> stack = new Stack<>();
for (int i = 0; i < chars.length; i++) {
if (chars[i] == '#') {
if (!stack.isEmpty()) {
stack.pop();
}
} else {
stack.push(chars[i]);
}
}
System.out.println(stack.size());
return stack;
}
}