let max_depth s =
  let d = ref 0 in
  let m = ref 0 in
  for i = 0 to String.length s - 1 do
    if s.[i] = '(' then begin
      d := !d + 1;
      if !d > !m then m := !d
    end
    else if s.[i] = ')' then d := !d - 1
  done;
  !m

;;

max_depth "((1+2)*(3-(4+5)))" + max_depth "(((deep)))" + max_depth "()()()"
