package com.fishercoder.solutions;

import java.util.Stack;

/**
 * 20. Valid Parentheses
 *
 * Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
 *
 * An input string is valid if:
 *
 * 1. Open brackets must be closed by the same type of brackets.
 * 2. Open brackets must be closed in the correct order.
 *
 * Note that an empty string is also considered valid.
 *
 * Example 1:
 * Input: "()"
 * Output: true
 *
 * Example 2:
 * Input: "()[]{}"
 * Output: true
 *
 * Example 3:
 * Input: "(]"
 * Output: false
 *
 * Example 4:
 * Input: "([)]"
 * Output: false
 *
 * Example 5:
 * Input: "{[]}"
 * Output: true
 * */
public class _20 {
    public static class Solution1 {
        public boolean isValid(String s) {
            Stack<Character> stack = new Stack<>();
            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();
        }
    }
}

@@ -1676,11 +1676,755 @@
  %7D%0A    %7D
+%0A%0A    public static class Solution2 %7B%0A        /**%0A         * A more concise solution:%0A         * credit: https://leetcode.com/problems/valid-parentheses/discuss/9178/Short-java-solution%0A         * */%0A        public boolean isValid(String s) %7B%0A            Stack%3CCharacter%3E stack = new Stack%3C%3E();%0A            for (char c : s.toCharArray()) %7B%0A                if (c == '(') %7B%0A                    stack.push(')');%0A                %7D else if (c == '%7B') %7B%0A                    stack.push('%7D');%0A                %7D else if (c == '%5B') %7B%0A                    stack.push('%5D');%0A                %7D else if (stack.isEmpty() %7C%7C stack.pop() != c) %7B%0A                    return false;%0A                %7D%0A            %7D%0A            return stack.isEmpty();%0A        %7D%0A    %7D
 %0A%7D%0A

add a more concise solution for 20