Skip to content

Commit a6f3032

Browse files
Create remove_all_adjacent_from_string.cpp
1 parent e954d56 commit a6f3032

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
};

0 commit comments

Comments
 (0)