solution to isBalanced