题目:删除字符串中的所有相邻重复项 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中只含有小写英文字母。
语言:java
public String removeDuplicates(String s, int k) {
StringBuilder sb = new StringBuilder(s);
int length = -1;
while (length != sb.length()) {
length = sb.length();
for (int i = 0, count = 1; i < sb.length(); ++i) {
if (i == 0 || sb.charAt(i) != sb.charAt(i - 1)) {                count = 1;
} else if (++count == k) {
sb.delete(i - k + 1, i + 1);
break;
}
}
}
String();
}
语言:java
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;
}
}
}
String();
}
语言:java
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);
}
}
}
字段字符串去重复String();
}
语言:cpp
string removeDuplicates(string s, int k) {
int length = -1;
while (length != s.size()) {
length = s.size();
for (int i = 0, count = 1; i < s.size(); ++i) {
if (i == 0 || s[i] != s[i - 1]) {
count = 1;
} else if (++count == k) {
break;
}
}
}
return s;
}
语言:cpp
string removeDuplicates(string s, int k) {
vector<int> count(s.size());
for (int i = 0; i < s.size(); ++i) {
if (i == 0 || s[i] != s[i - 1]) {
count[i] = 1;
} else {
count[i] = count[i - 1] + 1;
if (count[i] == k) {
i = i - k;
}
};
}
return s;
}
语言:cpp
string removeDuplicates(string s, int k) {
stack<int> counts;
for (int i = 0; i < s.size(); ++i) {
if (i == 0 || s[i] != s[i - 1]) {
counts.push(1);
} else if (++p() == k) {
counts.pop();
i = i - k;
}
}
return s;
}
语言:python
class Solution(object):
def removeDuplicates(self, s, k):
queue = deque([[None, 0]])
for x in s:
if queue[-1][0] != x:
queue.append([x, 1])
else:
queue[-1][1] +=1
if queue[-1][1] == k:
queue.pop()
res =""
while queue:
a, b = queue.popleft()
if a:
res += a*b
return res
## 语言:python
```python
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
语言:javascript
/**
* @param{string} s
* @param{number} k
* @return {string}
*/
var removeDuplicates =function(s, k) {
const reg =new RegExp("([a-z])\\1{"+ String(k -1) +"}");
while ((s)) {
s = s.replace(reg,"");
}
return s;
};
语言:javascript
/
**
* @param{string} s
* @param{number} k
* @return {string}
*/
var removeDuplicates =function (s, k) {
let stack = [] //字母栈
let countStack = [] //数字栈
let i =0
while(i < s.length){
if(stack[stack.length-1] == s[i]){
stack.push(s[i])
countStack[countStack.length-1] +=1 if(countStack[countStack.length-1] == k){
for(let j=0; j < k;j++){ //字母栈出栈
stack.pop()
}
countStack.pop() //数字栈出栈
}
}else{
stack.push(s[i])
countStack.push(1)
}
i++
}
return stack.join('')
};
语言:php
class S olution {
/**
* @param String $s
* @param Integer $k
* @return String
*/
function removeDuplicates($s,$k) {
//前面一个字符串
$pre=$s[0];
//数量
$num=1;
$len=strlen($s);
//判断字符串中间是否有过删除操作
$flag=0;
$res='';
for($i=1;$i<strlen($s);$i++){

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。