Skip to content

Commit a76b2a2

Browse files
committed
Added new solution to 1041
1 parent f4f50a2 commit a76b2a2

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

leetcode/cpp/Stacks/1041.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//1047. Remove All Adjacent Duplicates In String(Leetcode)
2+
//Solution using Stack data structure
3+
class Solution {
4+
public:
5+
string removeDuplicates(string S) {
6+
reverse(S.begin(),S.end()); //reversing input string because stacks are LIFO
7+
string output = ""; //Last In First Out
8+
stack <char> st;
9+
for(auto x : S){
10+
if(!(st.empty()) && x == st.top() ){
11+
st.pop();
12+
}else{
13+
st.push(x);
14+
}
15+
}
16+
while(!(st.empty())){
17+
output+=st.top();
18+
st.pop();
19+
}
20+
return output;
21+
}
22+
};

0 commit comments

Comments
 (0)