Advertisement
sajid161

11:4

Jan 8th, 2021
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.59 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     string removeDuplicates(string S) {
  4.       stack<char>st;
  5.     for(auto u:S)
  6.     {
  7.         st.push(u);
  8.         if(st.size()>=2)
  9.         {
  10.            int a,b;
  11.             a=st.top();
  12.             st.pop();
  13.             b=st.top();
  14.             st.pop();
  15.             if(a!=b)
  16.             {
  17.                 st.push(b);
  18.                 st.push(a);
  19.             }
  20.         }
  21.     }
  22.       string s1;
  23.     while(!st.empty())
  24.     {
  25.         char c;
  26.         c=st.top();
  27.         s1+=c;
  28.         st.pop();
  29.     }
  30.     reverse(s1.begin(),s1.end());
  31.    return s1;
  32.     }
  33. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement