Given a string of brackets, return the count of brackets that are properly matched
We dont need close variable if we can decrease open by 1 when close bracket is found. 

public class MatchBrackets{

    public static int matchBrackets(String brackets){

      int open = 0;
      int count =0;

     for(int i=0;i&lt;brackets.length();i++){
        if(brackets.charAt(i)=='('){
        open++;
        }else if(brackets.charAt(i)==')'){
         open--;
        }
        if(open&lt;0){
        count++;
        open++;
     }
  }
  return count+open;
}

  public static void main(String[] args) {

  System.out.println(matchBrackets("())("));
  System.out.println(matchBrackets("(((())))"));
  System.out.println(matchBrackets("))))"));
}}