package fundamentals.stack.validation.lc020_validparentheses;

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 Solution {

    // My 3AC: simplified further!
    public boolean isValid(String str) {
        Stack<Character> s = new Stack<>();
        for (char c : str.toCharArray()) {
            if (c == '(' || c == '{' || c == '[') {
                s.push(c);
            } else {
                if (s.isEmpty() ||
                        (c == ')' && s.pop() != '(') ||
                        (c == '}' && s.pop() != '{') ||
                        (c == ']' && s.pop() != '['))
                    return false;
            }
        }
        return s.isEmpty();
    }

    // A Simple one!
    public boolean isValid_simple(String s) {
        Stack<Character> stack = new Stack<>();
        for (char c : s.toCharArray()) {
            if (c == '(' || c == '{' || c == '[') {
                stack.push(c);
            } else {
                if (stack.isEmpty()) return false;
                else if (c == ')' && stack.pop() != '(') return false;
                else if (c == '}' && stack.pop() != '{') return false;
                else if (c == ']' && stack.pop() != '[') return false;
                /* else true */
            }
        }
        return stack.isEmpty();
    }

    // My 2AC
    public boolean isValid2(String s) {
        Stack<Character> stack = new Stack<>();
        for (char c : s.toCharArray()) {
            switch (c) {
                case '(':
                case '{':
                case '[':
                    stack.push(c);
                    break;
                case ')':
                    if (stack.isEmpty() || stack.pop() != '(') {
                        return false;
                    }
                    break;
                case ']':
                    if (stack.isEmpty() || stack.pop() != '[') {
                        return false;
                    }
                    break;
                case '}':
                    if (stack.isEmpty() || stack.pop() != '{') { // error2: stack could be empty
                        return false;
                    }
                    break;
                default:
                    return false;
            }
        }
        return stack.isEmpty(); // error1: don't return true directly!
    }

    // My 1AC
    public boolean isValid1(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            switch (c) {
                case '(':
                case '{':
                case '[':
                    stack.push(c);
                    break;
                case ')':
                    if (stack.isEmpty() || stack.pop() != '(') {
                        return false;
                    }
                    break;
                case '}':
                    if (stack.isEmpty() || stack.pop() != '{') {
                        return false;
                    }
                    break;
                case ']':
                    if (stack.isEmpty() || stack.pop() != '[') {
                        return false;
                    }
                    break;
                default:
                    return false;
            }
        }
        return stack.isEmpty();
    }

}

@@ -346,16 +346,419 @@
 tion %7B%0A%0A
+    public boolean isValid(String str) %7B%0A        Stack%3CCharacter%3E s = new Stack%3C%3E();%0A        for (char c : str.toCharArray()) %7B%0A            if (%22(%5B%7B%22.indexOf(c) %3E= 0) s.push(c);%0A            else if (s.isEmpty() %7C%7C (c == ')' && s.pop() != '(') %7C%7C%0A                    (c == '%5D' && s.pop() != '%5B') %7C%7C (c == '%7D' && s.pop() != '%7B'))%0A                return false;%0A        %7D%0A        return s.isEmpty();%0A    %7D%0A%0A
     // M
@@ -802,32 +802,33 @@
  boolean isValid
+3
 (String str) %7B%0A 

valid parentheses