jayati

Removing Stars From a String

Apr 10th, 2023
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.52 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     string removeStars(string s) {
  4.         stack<int> st;
  5.         string s1="";
  6.         for(int i=0;i<s.length();i++)
  7.         {
  8.             if(s[i]!='*')
  9.             {
  10.                 st.push(s[i]);
  11.             }
  12.             else if(s[i]=='*' && !st.empty())
  13.             {
  14.                 st.pop();
  15.             }
  16.         }
  17.         while(!st.empty())
  18.         {
  19.            s1+=st.top();
  20.            st.pop();
  21.         }
  22.         reverse(s1.begin(),s1.end());
  23.        return s1;
  24.     }
  25. };
Advertisement
Add Comment
Please, Sign In to add comment