package com.fishercoder.solutions;

import java.util.Stack;

/**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 boolean isValid(String s) {
        Stack<Character> stack = new Stack();
        char[] schar = s.toCharArray();
        for(int i = 0; i < schar.length; i++){
            if(schar[i] == '(' || schar[i] == '[' || schar[i] == '{') stack.push(schar[i]);
            else if(schar[i] == ')' || schar[i] == ']' || schar[i] == '}'){
                if(stack.isEmpty()) return false;
                else {
                    char pop = stack.pop();
                    if(schar[i] == ')' && pop != '(') return false;
                    else if(schar[i] == ']' && pop != '[') return false;
                    else if(schar[i] == '}' && pop != '{') return false;
                }
            }
        }
        return stack.isEmpty();
    }

}

@@ -50,19 +50,80 @@
 til.
-Stack;%0A%0A/**
+ArrayDeque;%0Aimport java.util.Deque;%0A%0A/**%0A * 20. Valid Parentheses%0A *%0A * 
 Give
@@ -237,17 +237,18 @@
  valid.%0A
-%0A
+ *
  The bra
@@ -416,21 +416,21 @@
         
-Stack
+Deque
 %3CCharact
@@ -449,53 +449,20 @@
 new 
-Stack();%0A        char%5B%5D schar = s.toCharArray
+ArrayDeque%3C%3E
 ();%0A
@@ -472,16 +472,17 @@
      for
+ 
 (int i =
@@ -494,25 +494,24 @@
  %3C s
-char
 .length
+()
 ; i++)
+ 
 %7B%0A  
@@ -518,33 +518,37 @@
           if
+ 
 (s
+.
 char
-%5Bi%5D
+At(i)
  == '(' %7C%7C s
@@ -551,42 +551,66 @@
 %7C%7C s
+.
 char
-%5Bi%5D
+At(i)
  == '
-%5B
+%7B
 ' %7C%7C s
+.
 char
-%5Bi%5D
+At(i)
  == '
-%7B
+%5B
 ')
+ %7B%0A               
  sta
@@ -618,23 +618,26 @@
 k.push(s
+.
 char
-%5Bi%5D
+At(i)
 );%0A     
@@ -646,71 +646,16 @@
     
+ %7D
  else 
-if(schar%5Bi%5D == ')' %7C%7C schar%5Bi%5D == '%5D' %7C%7C schar%5Bi%5D == '%7D')
 %7B%0A  
@@ -670,16 +670,17 @@
       if
+ 
 (stack.i
@@ -750,83 +750,52 @@
     
-char pop = stack.pop();%0A                    if(schar%5Bi%5D == ')' && pop
+if (stack.peek() == '(' && s.charAt(i)
  != '
-(
+)
 ') r
@@ -838,37 +838,50 @@
 e if
+ 
 (s
-char%5Bi%5D
+tack.peek()
  == '
-%5D
+%7B
 ' && 
-pop
+s.charAt(i)
  != '
-%5B
+%7D
 ') r
@@ -924,37 +924,50 @@
 e if
+ 
 (s
-char%5Bi%5D
+tack.peek()
  == '
-%7D
+%5B
 ' && 
-pop
+s.charAt(i)
  != '
-%7B
+%5D
 ') r
@@ -971,32 +971,65 @@
 ) return false;%0A
+                    stack.pop();%0A
                 
@@ -1092,11 +1092,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

edit 20