package com.fishercoder.solutions;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * 20. Valid Parentheses
 *
 * Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
 * The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.*/
public class _20 {

    public static class Solution1 {
        public boolean isValid(String s) {
            Deque<Character> stack = new ArrayDeque<>();
            for (int i = 0; i < s.length(); i++) {
                if (s.charAt(i) == '(' || s.charAt(i) == '{' || s.charAt(i) == '[') {
                    stack.push(s.charAt(i));
                } else {
                    if (stack.isEmpty()) {
                        return false;
                    } else {
                        if (stack.peek() == '(' && s.charAt(i) != ')') {
                            return false;
                        } else if (stack.peek() == '{' && s.charAt(i) != '}') {
                            return false;
                        } else if (stack.peek() == '[' && s.charAt(i) != ']') {
                            return false;
                        }
                        stack.pop();
                    }
                }
            }
            return stack.isEmpty();
        }
    }
}

@@ -50,42 +50,13 @@
 til.
-ArrayDeque;%0Aimport java.util.Deque
+Stack
 ;%0A%0A/
@@ -214,12 +214,119 @@
 .%0A *
- The
+%0A * An input string is valid if:%0A *%0A * 1. Open brackets must be closed by the same type of brackets.%0A * 2. Open
  bra
@@ -336,21 +336,25 @@
 ts must 
+be 
 close
+d
  in the 
@@ -370,70 +370,321 @@
 rder
-, %22()%22 and %22()%5B%5D%7B%7D%22 are all valid b
+.%0A *%0A * Note that an empty string is also considered valid.%0A *%0A * Example 1:%0A * Input: %22()%22%0A * Output: true%0A *%0A * Example 2:%0A * Input: %22()%5B%5D%7B%7D%22%0A * Output: true%0A *%0A * Example 3:%0A * Inp
 ut
+:
  %22(%5D%22
- and %22(%5B)%5D%22 are not.
+%0A * Output: false%0A *%0A * Example 4:%0A * Input: %22(%5B)%5D%22%0A * Output: false%0A *%0A * Example 5:%0A * Input: %22%7B%5B%5D%7D%22%0A * Output: true%0A * 
 */%0Ap
@@ -701,17 +701,16 @@
 s _20 %7B%0A
-%0A
     publ
@@ -792,21 +792,21 @@
         
-Deque
+Stack
 %3CCharact
@@ -825,18 +825,13 @@
 new 
-ArrayDeque
+Stack
 %3C%3E()

refactor 20