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

时间:2019-08-16 13:19:51

标签: string

从字符串中删除myd$group个连续的相同字符,直到该字符串没有任何连续的字符。

'k'

5 个答案:

答案 0 :(得分: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;
}};

答案 1 :(得分:0)

一种基于正则表达式的方法是迭代并继续用空字符串替换模式(.)\1+,直到输入字符串的长度不再变小为止,这意味着无法删除更多的重复项。这是Java中的工作脚本:

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

此处的输出仅为c

如果要基于输入k控制替换次数,则只需使用for循环即可:

String input = "abbac";
int k = 3;
for (int i=0; i < k; ++i) {
    input = input.replaceAll("(.)\\1+", "");
}

答案 2 :(得分:0)

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;
}

答案 3 :(得分:0)

#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;
}

答案 4 :(得分: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);

    }
}