class Solution {
public:
string removeDuplicates(string s, int k) {
}
};
1209. 删除字符串中的所有相邻重复项 II
给你一个字符串 s
,「k
倍重复项删除操作」将会从 s
中选择 k
个相邻且相等的字母,并删除它们,使被删去的字符串的左侧和右侧连在一起。
你需要对 s
重复进行无限次这样的删除操作,直到无法继续为止。
在执行完所有删除操作后,返回最终得到的字符串。
本题答案保证唯一。
示例 1:
输入:s = "abcd", k = 2 输出:"abcd" 解释:没有要删除的内容。
示例 2:
输入:s = "deeedbbcccbdaa", k = 3 输出:"aa" 解释: 先删除 "eee" 和 "ccc",得到 "ddbbbdaa" 再删除 "bbb",得到 "dddaa" 最后删除 "ddd",得到 "aa"
示例 3:
输入:s = "pbbcggttciiippooaais", k = 2 输出:"ps"
提示:
1 <= s.length <= 10^5
2 <= k <= 10^4
s
中只含有小写英文字母。原站题解
javascript 解法, 执行用时: 184 ms, 内存消耗: 48.9 MB, 提交时间: 2022-12-17 15:57:22
/** * @param {string} s * @param {number} k * @return {string} */ var removeDuplicates = function(s, k) { let stack = [] for(let c of s) { let prev = stack.pop() if(!prev || prev[0] !== c) { stack.push(prev) stack.push(c) } else if(prev.length < k-1) { stack.push(prev+c) } } return stack.join('') };
python3 解法, 执行用时: 88 ms, 内存消耗: 19.8 MB, 提交时间: 2022-12-17 15:57:01
class Solution: def removeDuplicates(self, s: str, k: int) -> str: n = len(s) stack = [] for c in s: if not stack or stack[-1][0] != c: stack.append([c, 1]) elif stack[-1][1] + 1 < k: stack[-1][1] += 1 else: stack.pop() ans = "" for c, l in stack: ans += c * l return ans
java 解法, 执行用时: 45 ms, 内存消耗: 42.1 MB, 提交时间: 2022-12-17 15:56:41
// 双指针 class Solution { public String removeDuplicates(String s, int k) { Stack<Integer> counts = new Stack<>(); char[] sa = s.toCharArray(); int j = 0; for (int i = 0; i < s.length(); ++i, ++j) { sa[j] = sa[i]; if (j == 0 || sa[j] != sa[j - 1]) { counts.push(1); } else { int incremented = counts.pop() + 1; if (incremented == k) { j = j - k; } else { counts.push(incremented); } } } return new String(sa, 0, j); } }
java 解法, 执行用时: 51 ms, 内存消耗: 42.4 MB, 提交时间: 2022-12-17 15:56:16
// 栈重建 class Solution { class Pair { int cnt; char ch; public Pair(int cnt, char ch) { this.ch = ch; this.cnt = cnt; } } public String removeDuplicates(String s, int k) { Stack<Pair> counts = new Stack<>(); for (int i = 0; i < s.length(); ++i) { if (counts.empty() || s.charAt(i) != counts.peek().ch) { counts.push(new Pair(1, s.charAt(i))); } else { if (++counts.peek().cnt == k) { counts.pop(); } } } StringBuilder b = new StringBuilder(); while (!counts.empty()) { Pair p = counts.pop(); for (int i = 0; i < p.cnt; i++) { b.append(p.ch); } } return b.reverse().toString(); } }
java 解法, 执行用时: 56 ms, 内存消耗: 42 MB, 提交时间: 2022-12-17 15:55:53
// 栈 class Solution { public String removeDuplicates(String s, int k) { StringBuilder sb = new StringBuilder(s); Stack<Integer> counts = new Stack<>(); for (int i = 0; i < sb.length(); ++i) { if (i == 0 || sb.charAt(i) != sb.charAt(i - 1)) { counts.push(1); } else { int incremented = counts.pop() + 1; if (incremented == k) { sb.delete(i - k + 1, i + 1); i = i - k; } else { counts.push(incremented); } } } return sb.toString(); } }
java 解法, 执行用时: 25 ms, 内存消耗: 42.1 MB, 提交时间: 2022-12-17 15:55:19
// 记忆计数法 class Solution { public String removeDuplicates(String s, int k) { StringBuilder sb = new StringBuilder(s); int count[] = new int[sb.length()]; for (int i = 0; i < sb.length(); ++i) { if (i == 0 || sb.charAt(i) != sb.charAt(i - 1)) { count[i] = 1; } else { count[i] = count[i - 1] + 1; if (count[i] == k) { sb.delete(i - k + 1, i + 1); i = i - k; } } } return sb.toString(); } }