/*
The program as a whole takes as an argument to the command
line a string. It displays the word "true" if it contains only matched
parentheses, false otherwise. processNestedParens advances past all
matching parens, "functionally" updating a position counter, and
isNestedParens checks the string (by seeing if processNestedParens
advances the whole way).
*/
public class Parens {
    public static int processNestedParens(String str, int pos) {
        while(pos < str.length()) {
            int oldPos = pos;
            if(str.charAt(pos) != '(') {
                return pos;
            }
            pos++;
            pos = processNestedParens(str, pos);
            if(pos >= str.length()) {
                return oldPos;
            }
            if(str.charAt(pos) != ')') {
                return oldPos;
            }
            pos++;
        }
        return pos;
    }


    /*
     *  Given a string of open and close parentheses, determine whether or not the parentheses are matched.
     */
    public static boolean isNestedParens(String str) {
        return processNestedParens(str, 0) == str.length();
    }

    public static void main(String [] args) {
        System.out.println(isNestedParens(args[0]));
    }
}
