class Solution {
          public boolean isValid(String s) {
              if (s.length() == 1) return false;
              Stack<Character> st = new Stack<>();
              for (int i = 0 ; i < s.length() ; ++i) {
                  char c = s.charAt(i);
                  if (c == '(' || c == '[' || c == '{') {
                      st.push(c);
                  }
                  else {
                      if (st.empty()) return false;
                      else if (st.peek() == '(' && c == ')') st.pop();
                      else if (st.peek() == '{' && c == '}') st.pop();
                      else if (st.peek() == '[' && c == ']') st.pop();
                      else return false;
                  }
              }
              return st.empty();
          }
      }
      
      Java language logo

      Valid Parentheses

      Check if the parentheses are valid or not

      0 Comments

        Add Comment

        Log in to add a comment

        Codiga - All rights reserved 2022.