-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathremove_all_adjacent_from_string_ii.cpp
48 lines (46 loc) · 1.85 KB
/
remove_all_adjacent_from_string_ii.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Solution {
private:
void removeHelper(string &s, int K) {
int n = s.length();
if(n < K ) return; // edgecase
// cout<<"\nCurrent string - "<<s<<"\n";
bool remove = false;
int position = 0;
int start = 0;
while(position <= n-K+1) {
if(s[position] == s[position+1]) { // if two characters match, then check for the next K characters
start = position; // initialize start pointer for removal
// cout<<"Start"<<start<<"\t";
int checkEqual = 1; // to check for K matches, the count of equal matches must be K - 1. Thus, 1 + K - 1 = K.
while(s[position] == s[position+1]) { // if all the next K characters match
++position, ++checkEqual;
if(checkEqual == K) break;
}
// cout<<"CheckEqual"<<checkEqual<<"\n";
if(checkEqual == K and start <= n-K) {
remove = true; // flag for recursive call
s.erase(s.begin() + start, s.begin() + start + K);
cout<<s<<" |##| "<<remove<<" | ";
position += K;
if(position == n) { // check for out of bounds
// cout<<"1\n";
break;
}
else
// cout<<"0\n";
}
}
else {
++position; //increment and check next char in case of mismatch
}
}
if(remove == false) return; //if no duplicate found or if string has become empty after recursive deletions
else if(remove == true) removeHelper(s, K);
}
public:
string removeDuplicates(string S, int k) {
if(S == "") return "";
removeHelper(S, k);
return S;
}
};