leetcode

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

0020.cpp (700B)


      1 class Solution {
      2     char is_open(char c) { return c == '(' || c == '[' || c == '{'; }
      3     char get_open_pair(char c) {
      4         switch (c) {
      5         case ')': return '(';
      6         case ']': return '[';
      7         case '}': return '{';
      8         default: return 0;
      9         }
     10     }
     11 
     12   public:
     13     bool isValid(string s) {
     14         stack<char> st;
     15 
     16         char t;
     17         for (auto c : s)
     18             if (is_open(c))
     19                 st.push(c);
     20             else if ((t = get_open_pair(c)))
     21                 if (!st.empty() && st.top() == t)
     22                     st.pop();
     23                 else
     24                     return false;
     25             else
     26                 return false;
     27 
     28         return st.empty();
     29     }
     30 };