Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit edd9dcc

Browse files
add 844
1 parent a010fb3 commit edd9dcc

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2424
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
25+
|844|[Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_844.java) | O(n) | O(1) | |Easy|
2526
|832|[Flipping an Image](https://leetcode.com/problems/flipping-an-image/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_832.java) | O(n) | O(1) | |Easy|
2627
|830|[Positions of Large Groups](https://leetcode.com/problems/positions-of-large-groups/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_830.java) | O(n) | O(n) | |Easy|
2728
|824|[Goat Latin](https://leetcode.com/problems/goat-latin/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_824.java) | O(n) | O(1) | |Easy|
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 844. Backspace String Compare
5+
*
6+
* Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character.
7+
*
8+
* Example 1:
9+
* Input: S = "ab#c", T = "ad#c"
10+
* Output: true
11+
* Explanation: Both S and T become "ac".
12+
*
13+
* Example 2:
14+
* Input: S = "ab##", T = "c#d#"
15+
* Output: true
16+
* Explanation: Both S and T become "".
17+
*
18+
* Example 3:
19+
* Input: S = "a##c", T = "#a#c"
20+
* Output: true
21+
* Explanation: Both S and T become "c".
22+
*
23+
* Example 4:
24+
* Input: S = "a#c", T = "b"
25+
* Output: false
26+
* Explanation: S becomes "c" while T becomes "b".
27+
* Note:
28+
*
29+
* 1 <= S.length <= 200
30+
* 1 <= T.length <= 200
31+
* S and T only contain lowercase letters and '#' characters.
32+
* Follow up:
33+
*
34+
* Can you solve it in O(N) time and O(1) space?
35+
*/
36+
public class _844 {
37+
public static class Solution1 {
38+
public boolean backspaceCompare(String S, String T) {
39+
String processedS = process(S);
40+
String processedT = process(T);
41+
return processedS.equals(processedT);
42+
}
43+
44+
private String process(String str) {
45+
StringBuilder sb = new StringBuilder();
46+
for (int i = 0; i < str.length(); i++) {
47+
if (str.charAt(i) == '#') {
48+
if (sb.length() > 0) {
49+
sb.deleteCharAt(sb.length() - 1);
50+
}
51+
} else {
52+
sb.append(str.charAt(i));
53+
}
54+
}
55+
return sb.reverse().toString();
56+
}
57+
}
58+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._844;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _844Test {
10+
private static _844.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _844.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(true, solution1.backspaceCompare("ab#c", "ad#c"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(true, solution1.backspaceCompare("ab##", "c#d#"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(true, solution1.backspaceCompare("a##c", "#a#c"));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(false, solution1.backspaceCompare("a#c", "b"));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)