String 从字符串中删除“k”个连续的相同字符,直到该字符串没有任何连续字符为止

String 从字符串中删除“k”个连续的相同字符,直到该字符串没有任何连续字符为止,string,String,从字符串中删除“k”个连续的相同字符,直到该字符串没有任何连续字符 example :- abbac k->2 in first iteration b's will be removed -> aac in second iteration a's will be removed -> c so output is -> c 一种基于正则表达式的方法是迭代并不断用空字符串替换模式。\1+,直到输入字符串的长度停止变小,这意味着不能再删除重复项。以下是一个Java

从字符串中删除“k”个连续的相同字符,直到该字符串没有任何连续字符

example :- abbac k->2
 in first iteration b's will be removed -> aac
 in second iteration a's will be removed -> c
so output is -> c 

一种基于正则表达式的方法是迭代并不断用空字符串替换模式。\1+,直到输入字符串的长度停止变小,这意味着不能再删除重复项。以下是一个Java工作脚本:

String input = "abbac";
int lastLength;
do {
    lastLength = input.length();
    input = input.replaceAll("(.)\\1+", "");
} while (input.length() < lastLength);
System.out.println(input);
}


您在这里使用什么语言/工具?到目前为止你都尝试了什么?欢迎来到stackoverflow。这不是免费的编码服务。请带上这本书,特别是阅读。然后回答您的问题并添加您迄今为止尝试过的代码。当你运行它时会发生什么?你以为会发生什么?有错误吗?祝你好运查看k=3时,字符串将保持原样->abbac@ashutoshyelgulwar很抱歉,我错过了k的要求,该要求控制替换的数量,q.v.我的更新答案。请编辑您的答案,包括如何回答问题的解释,以便人们可以从中学习。虽然此代码片段可以解决问题,它没有解释为什么或者如何回答这个问题。请,因为这确实有助于提高你的文章质量。请记住,您将在将来回答读者的问题,这些人可能不知道您的代码建议的原因。您可以使用该按钮改进此答案,以获得更多选票和声誉!
String input = "abbac";
int k = 3;
for (int i=0; i < k; ++i) {
    input = input.replaceAll("(.)\\1+", "");
}
string removeDuplicates(string str, int k) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    stack<pair<char,int> >s;
    for(int i=0;i<str.size();i++)
    {
        if(s.empty())s.push({str[i],1});
        else{
            
            if(s.top().first==str[i]){
                ++s.top().second;
                if(s.top().second==k){s.pop();}
            }
            else {s.push({str[i],1});}             
        }
    }
    string res="";
    while(!s.empty())
    {
        int x=s.top().second;
        while(x--)
            res=s.top().first+res;
        s.pop();
    }
    return res;
}};
string Reduced_String(long long k, string a){

    stack<pair<char,int>> s;
    int count=0;
    int i,x,n=a.size();
    
    for(i=0;i<n;i++)
    {
        if(s.size())
        {
            pair<char,int> p=s.top();
            if(a[i]==p.first)
            {
                count=p.second+1;
            }
            else
            {
                count=1;
            }
            
            s.push({a[i],count});
        }
        else
        {
            count=1;
            s.push({a[i],count});
        }
        
        pair<char,int> p=s.top();
        
        if(p.second>=k)
        {
            x=k;
            while(x--)
            {
                s.pop();
            }
        }
        
    }
    
    string ans="";
    
    while(s.size())
    {
        pair<char,int> p=s.top();
        ans.push_back(p.first);
        s.pop();
    }

    reverse(ans.begin(), ans.end());
    
    return and;
}
#include<bits/stdc++.h>
using namespace std;

string remove_K_Consecutive_Identical_Characters(string s,int k){
stack<pair<char,int> > st;
for(int i=0;i<s.size();i++){
    if(st.empty() || st.top().first!=s[i]){
        st.push({s[i],1});
    }
    else if(st.top().first==s[i]){
        pair<char,int> p=st.top();
        st.pop();
        p.second+=1;
        st.push(p);
        if(st.top().second==k){
            st.pop();
        }
    }
}
string result="";
while(!st.empty()){
    pair<char,int> p=st.top();
    for(int i=0;i<p.second;i++){
        result+=p.first;
    }
    st.pop();
} 
reverse(result.begin(),result.end());
return result;
int main(){
    string s="“qddxxxdaaa”";
    string result=remove_K_Consecutive_Identical_Characters(s,3);
    cout<<result<<endl;
    return 0;
}
class Result {
    public static String recursiveRemoveKconsuChar(String word, int k) {
        char ch=' ';int count =1;
        for(int i=0;i<word.length();i++) {
            if(word.charAt(i)== ch) {
                count++;
            }else {
                count =1;
            }
            if(count==k) {
                return word.substring(0,i-k+1)+ word.substring(i+1,word.length());
            }
            ch = word.charAt(i);
        }
        return word;
    }

    public static String compressWord(String word, int k) {
        while(true) {
            String ret = recursiveRemoveKconsuChar(word, k);
            if(ret.equals(word)) {
                return ret;
            }
            word = ret;
        }
    }
}

public class RemoveConsicutiveKString {
    public static void main(String[] args) throws IOException {
        String result = Result.compressWord("aba", 2);
        System.out.println(result);

    }
}