Interview bit is giving wrong statement


#1

#include<bits/stdc++.h>
string Solution::solve(string s, int k) {
int i=0;
int ct=1;
map<int,int> mp;
int n = s.length();
for(i=1;i<n;i++)
{
if(s[i]==s[i-1])
{
ct++;
}
else
{
if(ct==k)
{
mp[i-1]=1;
}
ct=1;
}
}
if(ct==k)
mp[n-1]=1;
string ans="";
for(i=n-1;i>=0;i–)
{
if(mp[i]==1)
{
for(int x=0;x<k;x++)
{
i–;
}
}
ans=s[i]+ans;
}
return ans;
}

Why this is happening??


#2

Why complicate things using maps and all. Use general string manipulation

string Solution::solve(string A, int B) 
{
    int i=1, c=1;
    char x=A[0];
    string temp="", ans="";
    temp+=x;
    while(i<A.length())
    {
        //cout<<A[i]<<" "<<x<<" "<<c<<endl;
        if(A[i]==x)
        {
            temp+=x;
            c++;
            i++;
        }
        else 
        {
            if(c!=B)
                ans+=temp;
            temp="";
            x=A[i];
            temp+=x;
            c=1;
            i++;
        }
    }
    if(c!=B)
        ans+=temp;
    return ans;
}


#3

I am also facing the same issue. Did you figure out the solution


#4

i am also facing same issue ???


#5

Exactly the same issue. I saw 2 cases where although my answers are correct, they popped up the Wrong Answer!