844. Backspace String Compare
by Botao Xiao
844. Backspace String Compare
Thinking:
- Method 1:
- 使用栈。
class Solution { public boolean backspaceCompare(String S, String T) { int sLen = S.length(); int tLen = T.length(); Stack<Character> st1 = new Stack<>(); Stack<Character> st2 = new Stack<>(); for(int i = 0; i < sLen; i++){ if(st1.size() == 0 && S.charAt(i) == '#') continue; if(S.charAt(i) == '#') st1.pop(); else st1.push(S.charAt(i)); } for(int i = 0; i < tLen; i++){ if(st2.size() == 0 && T.charAt(i) == '#') continue; if(T.charAt(i) == '#') st2.pop(); else st2.push(T.charAt(i)); } if(st1.size() != st2.size()) return false; while(!st1.isEmpty()){ if(st1.pop() != st2.pop()) return false; } return true; } }
- 使用栈。
Subscribe via RSS