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
| class Solution { public boolean backspaceCompare(String S, String T) { Stack<Character> stack1 = getStack(S); Stack<Character> stack2 = getStack(T); if (stack1.size() != stack2.size()) { return false; }
while (!stack1.isEmpty()) { if (stack1.pop() != stack2.pop()) { return false; } } return true; }
public Stack<Character> getStack(String S){ Stack<Character> stack = new Stack<>(); for (int i = 0; i < S.length(); i++) { if (S.charAt(i) == '#') { if (!stack.isEmpty()) { stack.pop(); } }else { stack.push(S.charAt(i)); } } return stack; } }
|