classSolution{ publicbooleanbackspaceCompare(String S, String T){ Deque<Character> stack1 = new ArrayDeque<>(); Deque<Character> stack2 = new ArrayDeque<>(); for (int i = 0; i < S.length(); i++) { if (S.charAt(i) == '#') { if (stack1.isEmpty()) continue; stack1.pop(); } else { stack1.push(S.charAt(i)); } } for (int i = 0; i < T.length(); i++) { if (T.charAt(i) == '#') { if (stack2.isEmpty()) continue; stack2.pop(); } else stack2.push(T.charAt(i)); } if (stack1.size() == stack2.size()) { while (!stack1.isEmpty()){ char a = stack1.pop(); char b = stack2.pop(); if (a != b) returnfalse; } returntrue; } returnfalse; } }