The input is first converted into bytes. This is safe as it contains only ASCII characters. Then each parenthesis is parsed into either +1 or -1, treating the trailing newline as a special case of 0.