Alan's notes from midterm review: problem statement: https://olio.cs.washington.edu/exams/viewer/question/56de05b9a62b97db12d839b3 If you have questions about this problem, feel free to post about it. I have posted here two different "solutions"; 1. Maybe "best" 2. Works and would be just fine on an exam! By the way, you don't need to comment for the exam, but just for fun, if you were to comment on this method, what would you say? :] 1) public static boolean isBalanced(String s) { int opens = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == ')') { // Closing! Check for openings! if (opens == 0) { return false; } else { // Used up one opening! opens--; } } else { // Must be opening! opens++; // Increment our stash of opening parens } } // Now we know we had enough closing parens, // But need to make sure no extra opening braces! if (opens == 0) { // Used up all openings, or none to begin with return true; } else { return false; } // Alternative boolean zen that Benjamin suggested (which is better) // return opens == 0; } 2) // Works fine! Definitely something I'd expect :] public static boolean isBalanced(String s) { // Check multiple cases in case not sure how to proceed if (s.equals("") || s.equals("()") || s.equals("(())") ... etc.) { return true; } // Remember to use the .equals() method for comparing Strings! int opens = 0; int closes = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == '(') { opens++; } else { closes++; } if (closes > opens) { // If we see more closing than opening, no way it's balanced return false; } } // Some boolean zen cause why not return opens == closes; /* the not-so-zen way if (opens == closes) { return true; } else { return youKnowWhat :]; } */ }