Advertisement
chevengur

leetcode | Valid Parentheses

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