Skip to content

Commit 1c395a2

Browse files
committed
844_Backspace_String_Compare
1 parent 9ba0b36 commit 1c395a2

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ Remember solutions are only solutions to given problems. If you want full study
151151
| 771 | [Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/771_Jewels_and_Stones.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/771_Jewels_and_Stones.java) | Count given char in string. Hash or table. [Oneline](https://leetcode.com/problems/jewels-and-stones/discuss/113574/1-liners-PythonJavaRuby) |
152152
| 804 | [Unique Morse Code Words](https://leetcode.com/problems/unique-morse-code-words/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/804_Unique_Morse_Code_Words.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/804_Unique_Morse_Code_Words.java) | String, Hash and Set. Set is recommended. |
153153
| 819 | [Most Common Word](https://leetcode.com/problems/most-common-word/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/819_Most_Common_Word.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/819_Most_Common_Word.java) | String processing, be careful about 'b,b,b'. regex is recommended. |
154+
| 844 | [Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/844_Backspace_String_Compare.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/844_Backspace_String_Compare.java) | 1. Stack pop when encounters #, O(n) and O(n)<br>2. Compare string from end to start, O(n) and O(1) |
154155
| 904 | [Fruit Into Baskets](https://leetcode.com/problems/fruit-into-baskets/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/904_Fruit_Into_Baskets.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/904_Fruit_Into_Baskets.java) | 1. Scan through blocks of tree, O(n) and O(n)<br>2. Mainten a sliding window with start and curr point, O(n) and O(n). |
155156
| 929 | [Unique Email Addresses](https://leetcode.com/problems/unique-email-addresses/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/929_Unique_Email_Addresses.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/929_Unique_Email_Addresses.java) | String handle and hash (or set) |
156157
| 945 | [Minimum Increment to Make Array Unique](https://leetcode.com/problems/minimum-increment-to-make-array-unique/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/945_Minimum_Increment_to_Make_Array_Unique.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/945_Minimum_Increment_to_Make_Array_Unique.java) | Sort, then list duplicate and missing value in sorted list. O(nlgn) and O(n) |
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
/*public boolean backspaceCompare(String S, String T) {
3+
// https://leetcode.com/problems/backspace-string-compare/discuss/135603/C%2B%2BJavaPython-O(N)-time-and-O(1)-space
4+
int i = S.length() - 1, j = T.length() - 1;
5+
while (true) {
6+
for (int back = 0; i >= 0 && (back > 0 || S.charAt(i) == '#'); --i)
7+
back += S.charAt(i) == '#' ? 1 : -1;
8+
for (int back = 0; j >= 0 && (back > 0 || T.charAt(j) == '#'); --j)
9+
back += T.charAt(j) == '#' ? 1 : -1;
10+
if (i >= 0 && j >= 0 && S.charAt(i) == T.charAt(j)) {
11+
i--; j--;
12+
} else
13+
return i == -1 && j == -1;
14+
}
15+
}*/
16+
17+
public boolean backspaceCompare(String S, String T) {
18+
return trans(S).equals(trans(T));
19+
}
20+
private String trans(String str) {
21+
StringBuilder sb = new StringBuilder();
22+
for (char c : str.toCharArray()) {
23+
if (c != '#') { sb.append(c); } // if not '#', append it at the end of sb.
24+
else if (sb.length() > 0) { sb.deleteCharAt(sb.length() - 1); } // remove last char in sb, if sb is not empty.
25+
}
26+
return sb.toString();
27+
}
28+
29+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class Solution(object):
2+
def backspaceCompare(self, S, T):
3+
"""
4+
:type S: str
5+
:type T: str
6+
:rtype: bool
7+
"""
8+
if S == T:
9+
return True
10+
s_stack = []
11+
t_stack = []
12+
for c in S:
13+
if c != '#':
14+
s_stack.append(c)
15+
elif len(s_stack) != 0:
16+
s_stack.pop(-1)
17+
for c in T:
18+
if c != '#':
19+
t_stack.append(c)
20+
elif len(t_stack) != 0:
21+
t_stack.pop(-1)
22+
return ''.join(s_stack) == ''.join(t_stack)
23+
24+
# def backspaceCompare(self, S, T):
25+
# # https://leetcode.com/problems/backspace-string-compare/discuss/135603/C%2B%2BJavaPython-O(N)-time-and-O(1)-space
26+
# back = lambda res, c: res[:-1] if c == '#' else res + c
27+
# return reduce(back, S, "") == reduce(back, T, "")
28+
29+
# def backspaceCompare(self, S, T):
30+
# def back(res, c):
31+
# if c != '#': res.append(c)
32+
# elif res: res.pop()
33+
# return res
34+
# return reduce(back, S, []) == reduce(back, T, [])
35+
36+
37+
# def backspaceCompare(self, S, T):
38+
# i, j = len(S) - 1, len(T) - 1
39+
# backS = backT = 0
40+
# while True:
41+
# while i >= 0 and (backS or S[i] == '#'):
42+
# backS += 1 if S[i] == '#' else -1
43+
# i -= 1
44+
# while j >= 0 and (backT or T[j] == '#'):
45+
# backT += 1 if T[j] == '#' else -1
46+
# j -= 1
47+
# if not (i >= 0 and j >= 0 and S[i] == T[j]):
48+
# return i == j == -1
49+
# i, j = i - 1, j - 1

0 commit comments

Comments
 (0)