File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed
Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+
3+ private:
4+ void removeHelper (string &s) {
5+ bool remove = false ;
6+ int n = s.length ();
7+ int position = 0 ;
8+
9+ while (position < n-1 ) {
10+ // cout<<s[position]<<s[position+1]<<"-->";
11+ if (s[position] == s[position+1 ]) {
12+ remove = true ;
13+ s.erase (s.begin () + position, s.begin () + position + 2 );
14+ // cout<<s<<"\n";
15+ if (position <= n)
16+ position+=2 ;
17+ else return ; // iterator out of bounds
18+ }
19+ else {
20+ ++position; // increment and check
21+ }
22+ }
23+
24+ if (s.length () == 0 || remove == false ) return ;
25+ else if (remove == true ) removeHelper (s);
26+ }
27+ public:
28+ string removeDuplicates (string S) {
29+ if (S == " " ) return " " ;
30+ removeHelper (S);
31+ return S;
32+
33+ }
34+
35+
36+
37+ };
You can’t perform that action at this time.
0 commit comments