leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1249.cpp (528B)


      1 class Solution {
      2   public:
      3     string minRemoveToMakeValid(string s) {
      4         stack<int> st;
      5         for (auto i = 0; i < s.size(); i++) {
      6             if (s[i] == '(')
      7                 st.push(i);
      8             else if (s[i] == ')') {
      9                 if (!st.empty())
     10                     st.pop();
     11                 else
     12                     s[i] = '*';
     13             }
     14         }
     15         while (!st.empty())
     16             s[st.top()] = '*', st.pop();
     17         s.erase(remove(s.begin(), s.end(), '*'), s.end());
     18         return s;
     19     }
     20 };