Guest User

Untitled

a guest
Dec 18th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. class Solution {
  2. public:
  3. bool isValid(string s) {
  4. stack<char> st;
  5.  
  6. for (const auto& c : s)
  7. {
  8. if (st.empty())
  9. {
  10. st.push(c);
  11. continue;
  12. }
  13.  
  14. if (checkPair(st.top(), c)) st.pop();
  15. else st.push(c);
  16. }
  17.  
  18. return st.empty();
  19. }
  20.  
  21. bool checkPair(const char& x, const char& y)
  22. {
  23. if (x=='{' && y=='}') return true;
  24. if (x=='(' && y==')') return true;
  25. if (x=='[' && y==']') return true;
  26.  
  27. return false;
  28. }
  29. };
Add Comment
Please, Sign In to add comment