Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
2,792 views

Image of Question : image

image

image

in Online Assessments by Expert (113,390 points)
edited by | 2,792 views

2 Answers

0 like 0 dislike
Best answer

C++ Code : 

#include <bits/stdc++.h>

using namespace std;
typedef long long int ll ; 
string s ; 
ll r ; 

bool check(ll kk){
    char yy = s[kk];
    ll k = s.size();ll g = 0 ; ll p = 0 ; 
    while(kk<k && p<r){
        p++;
        if(s[kk]==yy){
            g++;
        }
        
        kk++;
    }
    
    if(g>=r){
        return true ; 
    }
    return false ; 
    
}

int main() {
   
    
    cin>>r;
    cin>>s;
   
    
    ll gg = 0 ; 
    while(gg==0){
        
        ll k = s.size();
        ll i = 0 ; ll vv = 0 ; 
        while(i<k){
            
            if(check(i)==true){
                
                string s1 = s.substr(0,i);
                ll v = i + r ; 
                ll l = abs(k-v);
                //cout<<v;
                //cout<<"\n";
                string s2 = s.substr(v,l);
                s = s1 + s2 ; 
                vv = 1 ; 
                i =  1e18 ; 
            }
            
            i++;
        }
        
        
        if(vv==1){
        
        } else {
            gg = 1 ; 
        }
        
        
    }
    cout<<s ; 
    
    
    
    return 0 ; 
}
by Expert (113,390 points)
0 like 0 dislike
#include<bits/stdc++.h>
using namespace std;

void solve(){
    string s;
    cin>>s;
    int n;
    cin>>n;
    int i=1;
    int cnt=1;
    string ans="";
    char c=s[0];
    ans+=s[0];
    while(i<s.length()){
        
        if(s[i]==ans.back()) cnt++;
        else cnt=1;
        ans+=s[i];
        if(cnt==n){
            while(cnt>0){
                ans.pop_back();
                cnt--;
            }
            cnt=1;
        }
        i++;
    }
    if(cnt==n){
        while(cnt>0){
                ans.pop_back();
                cnt--;
        }
    }
    cout<<ans<<endl;
}
int main(){
    solve();
}

by (140 points)
edited by
0 0
Perfect bro..