Skip to content

Commit 186b5d9

Browse files
add 1047
1 parent a779d44 commit 186b5d9

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

README.md

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

2828
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2929
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
30+
|1047|[Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1047.java) | O(n) | O(1) | |Easy||
3031
|1037|[Valid Boomerang](https://leetcode.com/problems/valid-boomerang/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1037.java) | O(1) | O(1) | |Easy|Math|
3132
|1033|[Moving Stones Until Consecutive](https://leetcode.com/problems/moving-stones-until-consecutive/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1033.java) | O(1) | O(1) | |Easy|Math|
3233
|1030|[Matrix Cells in Distance Order](https://leetcode.com/problems/matrix-cells-in-distance-order/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1030.java) | O(R*C) | O(1) | |Easy|
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1047. Remove All Adjacent Duplicates In String
5+
*
6+
* Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent and equal letters, and removing them.
7+
*
8+
* We repeatedly make duplicate removals on S until we no longer can.
9+
*
10+
* Return the final string after all such duplicate removals have been made. It is guaranteed the answer is unique.
11+
*
12+
*
13+
*
14+
* Example 1:
15+
*
16+
* Input: "abbaca"
17+
* Output: "ca"
18+
* Explanation:
19+
* For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move.
20+
* The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca".
21+
*
22+
*
23+
* Note:
24+
*
25+
* 1 <= S.length <= 20000
26+
* S consists only of English lowercase letters.*/
27+
public class _1047 {
28+
public static class Solution1 {
29+
public String removeDuplicates(String S) {
30+
StringBuilder sb = new StringBuilder(S);
31+
for (int i = 0; i < S.length() - 1; i++) {
32+
if (S.charAt(i) == S.charAt(i + 1)) {
33+
return removeDuplicates(S.substring(0, i) + S.substring(i + 2));
34+
}
35+
}
36+
return sb.toString();
37+
}
38+
}
39+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1047;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1047Test {
10+
private static _1047.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1047.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals("ca", solution1.removeDuplicates("abbaca"));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)