This page is still under construction.

Parts of this page are still being built. What you see may change.

Correct Parentheses

Interview

Time limit1sMemory limit1024 MB

Summary
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 SS consisting of (\texttt{(} and )\texttt{)}, 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.

  1. ()\texttt{()} is a correct parenthesis sequence.
  2. If A\texttt{A} is a correct parenthesis sequence, then (A)\texttt{(A)} is a correct parenthesis sequence.
  3. If A\texttt{A} and B\texttt{B} are correct parenthesis sequences, then AB\texttt{AB} is a correct parenthesis sequence.

Input

The first line gives the string SS with no spaces. (3≤∣S∣≤100 0003 \leq \vert S \vert \leq 100\,000, and ∣S∣\vert S \vert is odd.)

The answer is at least 11. 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.

Examples2

  1. Example 1

    Input
    ()(()
    
    Expected output
    2
    
  2. Example 2

    Input
    ()(()))
    
    Expected output
    4