Remove Consecutive Characters
Given a string A and integer B, remove all consecutive same characters that have length exactly B.
A = "aabcd" B = 2
op:
"bcd"
string Solution::solve(string A, int B) {
string ans="";
int i=0;
int count=0;
string t="";
for(i=0;i<A.length();i++){
t=t+A[i];
count++;
if(A[i]!=A[i+1] )
{
if(count!=B)
ans=ans+t;
t="";
count=0;
}
}
return ans+t;
}
Comments
Post a Comment