Skip to content

Commit fc745cb

Browse files
committed
implement removeDuplicates function to eliminate adjacent duplicate characters in a string
1 parent b028249 commit fc745cb

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
string removeDuplicates(string s) {
4+
stack<char>st;
5+
for(auto ch:s){
6+
if(st.empty()){
7+
st.push(ch);
8+
continue;
9+
}
10+
if(st.top() !=ch){
11+
st.push(ch);
12+
}else{
13+
st.pop();
14+
}
15+
}
16+
string new_s ="";
17+
while(!st.empty()){
18+
char ch = st.top();
19+
new_s+=ch;
20+
st.pop();
21+
}
22+
reverse(new_s.begin(),new_s.end());
23+
return new_s;
24+
}
25+
};

0 commit comments

Comments
 (0)