Problem list
Log in
#usaco0056 · USACO 2012-2013 November Bronze

Typo

Gold II 2.0 s 256 MB

Bessie has just purchased a new laptop computer, but she unfortunately finds herself unable to type well, given the size of her large hooves relative to the small keyboard. Bessie has just attempted to type in one of her favorite patterns -- a balanced string of parentheses. However, she realizes that she might have mis-typed one character, accidentally replacing ( with ) or vice versa. Please help Bessie compute the number of locations in the string such that reversing the single parenthesis at that location would cause the entire string to become balanced.

A string of parentheses is "balanced" if there are the same total number of ('s and )'s, and for any prefix of the string there are at least as many ('s as )'s.

Input

Line 1: A string of parentheses of length N (1 <= N <= 100,000).

Output

The number of positions within the input string (if any) such that reversing the parenthesis at that single position would cause the entire string to become balanced.

Example 1

Input
()(())))
Output
4

Output details: Reversing the parenthesis at position 2, 5, 6, or 7 results in a balanced string.

Source
USACO 2012-2013 November Bronze
Appears in
Not part of a ladder, contest or set yet.
Accepted 0 Submissions 0 Acceptance 0% Solvers 0

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code