Star in Parentheses
InterviewTime limit2sMemory limit512 MB
Given balanced parentheses with one star, count the matching pairs that enclose the star's position.
- Level
Easy3 of 10
- Topics
- Stack, String, Implementation
- Solved
- No attempts yet
Problem
You are given a string , which is balanced parentheses with a star symbol * inserted.
Any balanced parentheses can be constructed using the following rules:
- An empty string is balanced.
- Concatenation of two balanced parentheses is balanced.
- If is balanced parentheses, concatenation of
(, , and)in this order is balanced.
For example, ()() and (()()) are balanced parentheses. )( and )()(() are not balanced parentheses.
Your task is to count how many matching pairs of parentheses surround the star.
Let be the -th character of a string . The pair of and is called a matching pair of parentheses if is (, is ) and the surrounded string by them is balanced when ignoring a star symbol.
Input
The input consists of a single test case formatted as follows.
S$S$
is balanced parentheses with exactly one * inserted somewhere. The length of is between and , inclusive.
Output
Print the answer in one line.