content: package org.redquark.tutorials.leetcode;
  
  import java.util.Stack;
  
  /**
   * @author Anirudh Sharma
   */
  public class ValidParentheses {
  
      private static boolean isValid(String s) {
          // Stack to store left symbols
          Stack<Character> leftSymbols = new Stack<>();
          // Loop for each character of the string
          for (char c : s.toCharArray()) {
              // If left symbol is encountered
              if (c == '(' || c == '{' || c == '[') {
                  leftSymbols.push(c);
              }
              // If right symbol is encountered
              else if (c == ')' && !leftSymbols.isEmpty() && leftSymbols.peek() == '(') {
                  leftSymbols.pop();
              } else if (c == '}' && !leftSymbols.isEmpty() && leftSymbols.peek() == '{') {
                  leftSymbols.pop();
              } else if (c == ']' && !leftSymbols.isEmpty() && leftSymbols.peek() == '[') {
                  leftSymbols.pop();
              }
              // If none of the valid symbols is encountered
              else {
                  return false;
              }
          }
          return leftSymbols.isEmpty();
      }
  
      public static void main(String[] args) {
          System.out.println(isValid("()"));
          System.out.println(isValid("()[]{}"));
          System.out.println(isValid("(]"));
          System.out.println(isValid("([)]"));
          System.out.println(isValid("{[]}"));
      }
  }
  