|
1 | | -class Solution { |
2 | | -private: |
3 | | - void removeHelper(string &s, int K) { |
4 | | - int n = s.length(); |
5 | | - if(n < K ) return; // edgecase |
6 | | - // cout<<"\nCurrent string - "<<s<<"\n"; |
| 1 | +// class Solution { |
| 2 | +// private: |
| 3 | +// void removeHelper(string &s, int K) { |
| 4 | +// int n = s.length(); |
| 5 | +// if(n < K ) return; // edgecase |
| 6 | +// // cout<<"\nCurrent string - "<<s<<"\n"; |
7 | 7 |
|
8 | | - bool remove = false; |
9 | | - int position = 0; |
10 | | - int start = 0; |
| 8 | +// bool remove = false; |
| 9 | +// int position = 0; |
| 10 | +// int start = 0; |
11 | 11 |
|
12 | | - while(position <= n-K+1) { |
13 | | - if(s[position] == s[position+1]) { // if two characters match, then check for the next K characters |
14 | | - start = position; // initialize start pointer for removal |
15 | | - // cout<<"Start"<<start<<"\t"; |
16 | | - int checkEqual = 1; // to check for K matches, the count of equal matches must be K - 1. Thus, 1 + K - 1 = K. |
17 | | - while(s[position] == s[position+1]) { // if all the next K characters match |
18 | | - ++position, ++checkEqual; |
19 | | - if(checkEqual == K) break; |
20 | | - } |
21 | | - // cout<<"CheckEqual"<<checkEqual<<"\n"; |
22 | | - if(checkEqual == K and start <= n-K) { |
23 | | - remove = true; // flag for recursive call |
24 | | - s.erase(s.begin() + start, s.begin() + start + K); |
25 | | - cout<<s<<" |##| "<<remove<<" | "; |
26 | | - position += K; |
27 | | - if(position == n) { // check for out of bounds |
28 | | - // cout<<"1\n"; |
29 | | - break; |
30 | | - } |
31 | | - else |
32 | | - // cout<<"0\n"; |
| 12 | +// while(position <= n-K+1) { |
| 13 | +// if(s[position] == s[position+1]) { // if two characters match, then check for the next K characters |
| 14 | +// start = position; // initialize start pointer for removal |
| 15 | +// // cout<<"Start"<<start<<"\t"; |
| 16 | +// int checkEqual = 1; // to check for K matches, the count of equal matches must be K - 1. Thus, 1 + K - 1 = K. |
| 17 | +// while(s[position] == s[position+1]) { // if all the next K characters match |
| 18 | +// ++position, ++checkEqual; |
| 19 | +// if(checkEqual == K) break; |
| 20 | +// } |
| 21 | +// // cout<<"CheckEqual"<<checkEqual<<"\n"; |
| 22 | +// if(checkEqual == K and start <= n-K) { |
| 23 | +// remove = true; // flag for recursive call |
| 24 | +// s.erase(s.begin() + start, s.begin() + start + K); |
| 25 | +// cout<<s<<" |##| "<<remove<<" | "; |
| 26 | +// position += K; |
| 27 | +// if(position == n) { // check for out of bounds |
| 28 | +// // cout<<"1\n"; |
| 29 | +// break; |
| 30 | +// } |
| 31 | +// else |
| 32 | +// // cout<<"0\n"; |
| 33 | +// } |
| 34 | +// } |
| 35 | +// else { |
| 36 | +// ++position; //increment and check next char in case of mismatch |
| 37 | +// } |
| 38 | +// } |
| 39 | +// if(remove == false) return; //if no duplicate found or if string has become empty after recursive deletions |
| 40 | +// else if(remove == true) removeHelper(s, K); |
| 41 | +// } |
| 42 | +// public: |
| 43 | +// string removeDuplicates(string S, int k) { |
| 44 | +// if(S == "") return ""; |
| 45 | +// removeHelper(S, k); |
| 46 | +// return S; |
| 47 | +// } |
| 48 | +// }; |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +//stack based solution |
| 53 | +class Solution { |
| 54 | +public: |
| 55 | + string removeDuplicates(string s, int k) { |
| 56 | + stack<pair<int,int>> st;//stack will hold (index, number of same character before the index including itself) |
| 57 | + |
| 58 | + for(int i = 0; i < s.size(); i++) { |
| 59 | + if(!st.empty() and s[st.top().first] == s[i]) { //if the current character matches with the character in the top of the stack |
| 60 | + if(st.top().second + 1 == k) { //if adding the current character in list , makes a list of size 'k' of same chatacters , |
| 61 | + int temp = k - 1; |
| 62 | + while(temp--) |
| 63 | + st.pop(); |
33 | 64 | } |
34 | | - } |
35 | | - else { |
36 | | - ++position; //increment and check next char in case of mismatch |
37 | | - } |
| 65 | + else //if it doesn't make a size of 'k' then push (index, number of same character) pair |
| 66 | + st.push({i,st.top().second + 1}); |
| 67 | + } |
| 68 | + else //if the stack is empty , push the current character index and number of same character , which is 1 |
| 69 | + st.push({i, 1}); |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + string result = ""; |
| 74 | + while(!st.empty()) { |
| 75 | + result += s[st.top().first]; |
| 76 | + st.pop(); |
38 | 77 | } |
39 | | - if(remove == false) return; //if no duplicate found or if string has become empty after recursive deletions |
40 | | - else if(remove == true) removeHelper(s, K); |
| 78 | + reverse(result.begin(), result.end()); |
| 79 | + return result; |
41 | 80 | } |
42 | | -public: |
43 | | - string removeDuplicates(string S, int k) { |
44 | | - if(S == "") return ""; |
45 | | - removeHelper(S, k); |
46 | | - return S; |
47 | | - } |
48 | 81 | }; |
0 commit comments