clueless coding // TODO: be smarter

LeetCode Valid Parentheses: Java Solution



TL;DR Code Solution


public boolean isValid(String s) {

    Stack<Character> stac = new Stack<Character>();

    for (int i = 0; i < s.length(); i++) {

        char cur = s.charAt(i);
        if (cur == '(')
            stac.push(')');
        else if (cur == '[')
            stac.push(']');
        else if (cur == '{')
            stac.push('}');
        else
            if (stac.isEmpty() || cur != stac.pop())
                return false;

    }
    return stac.isEmpty();

}