Skip to content

Commit e51b36e

Browse files
add 1209
1 parent 8d43b4b commit e51b36e

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ _If you like this project, please leave me a star._ ★
6161
|1217|[Play with Chips](https://leetcode.com/problems/play-with-chips/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1217.java) | |Easy|Array, Math, Greedy|
6262
|1214|[Two Sum BSTs](https://leetcode.com/problems/two-sum-bsts/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1214.java) | |Medium| Binary Search Tree|
6363
|1213|[Intersection of Three Sorted Arrays](https://leetcode.com/problems/intersection-of-three-sorted-arrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1213.java) | [:tv:](https://www.youtube.com/watch?v=zceoOrHSHNQ)|Easy||
64+
|1209|[Remove All Adjacent Duplicates in String II](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1207.java) ||Medium|Stack|
6465
|1207|[Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1207.java) | [:tv:](https://www.youtube.com/watch?v=_NYimlZY1PE)|Easy||
6566
|1200|[Minimum Absolute Difference](https://leetcode.com/problems/minimum-absolute-difference/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1200.java) | [:tv:](https://www.youtube.com/watch?v=mH1aEjOEjcQ)|Easy||
6667
|1198|[Find Smallest Common Element in All Rows](https://leetcode.com/problems/find-smallest-common-element-in-all-rows/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1198.java) | [:tv:](https://www.youtube.com/watch?v=RMiofZrTmWo)|Easy||
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* 1209. Remove All Adjacent Duplicates in String II
7+
*
8+
* Given a string s, a k duplicate removal consists of choosing k adjacent and equal
9+
* letters from s and removing them causing the left and the right side of the deleted substring to concatenate together.
10+
* We repeatedly make k duplicate removals on s until we no longer can.
11+
* Return the final string after all such duplicate removals have been made.
12+
* It is guaranteed that the answer is unique.
13+
*
14+
* Example 1:
15+
* Input: s = "abcd", k = 2
16+
* Output: "abcd"
17+
* Explanation: There's nothing to delete.
18+
*
19+
* Example 2:
20+
* Input: s = "deeedbbcccbdaa", k = 3
21+
* Output: "aa"
22+
* Explanation:
23+
* First delete "eee" and "ccc", get "ddbbbdaa"
24+
* Then delete "bbb", get "dddaa"
25+
* Finally delete "ddd", get "aa"
26+
*
27+
* Example 3:
28+
* Input: s = "pbbcggttciiippooaais", k = 2
29+
* Output: "ps"
30+
*
31+
* Constraints:
32+
* 1 <= s.length <= 10^5
33+
* 2 <= k <= 10^4
34+
* s only contains lower case English letters.
35+
* */
36+
public class _1209 {
37+
public static class Solution1 {
38+
public String removeDuplicates(String s, int k) {
39+
Stack<Character> stack = new Stack<>();
40+
char c = s.charAt(0);
41+
stack.push(c);
42+
int count = 1;
43+
for (int i = 1; i < s.length(); i++) {
44+
if (s.charAt(i) == c) {
45+
count++;
46+
stack.push(s.charAt(i));
47+
if (count == k) {
48+
while (!stack.isEmpty() && stack.peek() == c) {
49+
stack.pop();
50+
}
51+
count = 0;
52+
if (!stack.isEmpty()) {
53+
c = stack.peek();
54+
while (!stack.isEmpty() && c == stack.peek()) {
55+
count++;
56+
stack.pop();
57+
}
58+
int tmp = count;
59+
while (tmp-- > 0) {
60+
stack.push(c);
61+
}
62+
}
63+
}
64+
} else {
65+
c = s.charAt(i);
66+
stack.push(s.charAt(i));
67+
count = 1;
68+
}
69+
}
70+
StringBuilder sb = new StringBuilder();
71+
while (!stack.isEmpty()) {
72+
sb.append(stack.pop());
73+
}
74+
return sb.reverse().toString();
75+
}
76+
}
77+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1209;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1209Test {
10+
11+
private static _1209.Solution1 solution1;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1209.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
assertEquals("abcd", solution1.removeDuplicates("abcd", 2));
21+
}
22+
23+
@Test
24+
public void test2() {
25+
assertEquals("aa", solution1.removeDuplicates("deeedbbcccbdaa", 3));
26+
}
27+
28+
@Test
29+
public void test3() {
30+
assertEquals("ps", solution1.removeDuplicates("pbbcggttciiippooaais", 2));
31+
}
32+
33+
}

0 commit comments

Comments
 (0)