|
| 1 | +--- |
| 2 | +id: remove-all-adjacent-duplicates-in-string-II |
| 3 | +title: Remove All Adjacent Duplicates in String II |
| 4 | +sidebar_label: 1209. Remove All Adjacent Duplicates in String II |
| 5 | +tags: |
| 6 | + - String |
| 7 | + - Stack |
| 8 | +description: "Solution to Leetcode 1209. Remove All Adjacent Duplicates in String II" |
| 9 | +--- |
| 10 | + |
| 11 | +## Problem Description |
| 12 | + |
| 13 | +You are given a string s and an integer k, a k duplicate removal consists of choosing k adjacent and equal letters from s and removing them, causing the left and the right side of the deleted substring to concatenate together. |
| 14 | + |
| 15 | +We repeatedly make k duplicate removals on s until we no longer can. |
| 16 | + |
| 17 | +Return the final string after all such duplicate removals have been made. It is guaranteed that the answer is unique. |
| 18 | + |
| 19 | +### Examples |
| 20 | + |
| 21 | +**Example 1:** |
| 22 | + |
| 23 | +``` |
| 24 | +Input: s = "abcd", k = 2 |
| 25 | +Output: "abcd" |
| 26 | +Explanation: There's nothing to delete. |
| 27 | +``` |
| 28 | + |
| 29 | +**Example 2:** |
| 30 | + |
| 31 | +``` |
| 32 | +Input: s = "deeedbbcccbdaa", k = 3 |
| 33 | +Output: "aa" |
| 34 | +Explanation: |
| 35 | +First delete "eee" and "ccc", get "ddbbbdaa" |
| 36 | +Then delete "bbb", get "dddaa" |
| 37 | +Finally delete "ddd", get "aa" |
| 38 | +``` |
| 39 | + |
| 40 | +### Constraints |
| 41 | + |
| 42 | +- `1 <= s.length <= 10^5` |
| 43 | +- `2 <= k <= 10^4` |
| 44 | + |
| 45 | +### Approach |
| 46 | + |
| 47 | +1. Save the character c and its count to the stack. |
| 48 | +2. If the next character c is same as the last one, increment the count. |
| 49 | +3. Otherwise push a pair (c, 1) into the stack. |
| 50 | +4. I used a dummy element ('#', 0) to avoid empty stack. |
| 51 | + |
| 52 | +### Complexity |
| 53 | + |
| 54 | +Time complexity: $O(n)$ |
| 55 | +Space complexity: $O(n)$ |
| 56 | + |
| 57 | +### Solution |
| 58 | + |
| 59 | +#### Code in Different Languages |
| 60 | + |
| 61 | +#### C++ |
| 62 | + |
| 63 | +```cpp |
| 64 | + string removeDuplicates(string s, int k) { |
| 65 | + vector<pair<int, char>> stack = {{0, '#'}}; |
| 66 | + for (char c: s) { |
| 67 | + if (stack.back().second != c) { |
| 68 | + stack.push_back({1, c}); |
| 69 | + } else if (++stack.back().first == k) |
| 70 | + stack.pop_back(); |
| 71 | + } |
| 72 | + string res; |
| 73 | + for (auto & p : stack) { |
| 74 | + res.append(p.first, p.second); |
| 75 | + } |
| 76 | + return res; |
| 77 | + } |
| 78 | +``` |
| 79 | +
|
| 80 | +#### JAVA |
| 81 | +
|
| 82 | +```java |
| 83 | + public String removeDuplicates(String s, int k) { |
| 84 | + int[] count = new int[s.length()]; |
| 85 | + StringBuilder sb = new StringBuilder(); |
| 86 | + for(char c : s.toCharArray()) { |
| 87 | + sb.append(c); |
| 88 | + int last = sb.length()-1; |
| 89 | + count[last] = 1 + (last > 0 && sb.charAt(last) == sb.charAt(last-1) ? count[last-1] : 0); |
| 90 | + if(count[last] >= k) sb.delete(sb.length()-k, sb.length()); |
| 91 | + } |
| 92 | + return sb.toString(); |
| 93 | + } |
| 94 | +``` |
| 95 | + |
| 96 | +#### PYTHON |
| 97 | + |
| 98 | +```python |
| 99 | + def removeDuplicates(self, s, k): |
| 100 | + stack = [['#', 0]] |
| 101 | + for c in s: |
| 102 | + if stack[-1][0] == c: |
| 103 | + stack[-1][1] += 1 |
| 104 | + if stack[-1][1] == k: |
| 105 | + stack.pop() |
| 106 | + else: |
| 107 | + stack.append([c, 1]) |
| 108 | + return ''.join(c * k for c, k in stack) |
| 109 | +``` |
| 110 | + |
| 111 | +### Complexity Analysis |
| 112 | + |
| 113 | +- Time Complexity: $O(n)$ |
| 114 | + |
| 115 | +- Space Complexity: $O(n)$ |
| 116 | + |
| 117 | +### References |
| 118 | + |
| 119 | +- **LeetCode Problem**: Remove All Adjacent Duplicates in String II |
0 commit comments