Skip to content

Commit ab2339a

Browse files
authored
Merge pull request larissalages#169 from Kavisha20340/master
Added new solution to Leetcode Problem 1047
2 parents f4f50a2 + e401bb3 commit ab2339a

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

leetcode/cpp/Stacks/1047.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)