We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 8752ba4 commit a499fe9Copy full SHA for a499fe9
Leetcode_30days/day9_BackspaceStringCompare.java
@@ -0,0 +1,26 @@
1
+'''
2
+Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character.
3
4
+class Solution {
5
+ public boolean backspaceCompare(String S, String T) {
6
+ if (S == null || T == null) return false;
7
+
8
+ return type(S).equals(type(T));
9
+ }
10
11
+ private String type(String s) {
12
+ Stack<Character> st = new Stack<>();
13
+ for (char c : s.toCharArray()) {
14
+ if (c == '#') {
15
+ if (!st.isEmpty()) st.pop();
16
+ } else {
17
+ st.push(c);
18
19
20
+ StringBuffer strb = new StringBuffer();
21
+ while (!stack.isEmpty()) {
22
+ strb.append(st.pop());
23
24
+ return strb.toString();
25
26
+}
0 commit comments