Correct Parentheses
InterviewTime limit1sMemory limit1024 MB
Count how many single-character deletions from a bracket string leave a correct balanced parenthesis sequence.
- Level
Medium6 of 10
- Topics
- String, Prefix sum, Greedy, Implementation
- Solved
- No attempts yet
Problem
For a string consisting of and , print the number of ways to delete exactly one parenthesis so that the result is a correct parenthesis sequence.
A correct parenthesis sequence is defined as follows.
- is a correct parenthesis sequence.
- If is a correct parenthesis sequence, then is a correct parenthesis sequence.
- If and are correct parenthesis sequences, then is a correct parenthesis sequence.
Input
The first line gives the string with no spaces. (, and is odd.)
The answer is at least . That is, at least one character exists whose deletion yields a correct parenthesis sequence.
Output
Print the number of ways to obtain a correct parenthesis sequence.