Concatenating Parenthesis Strings
Time limit2sMemory limit128 MB
Decide whether the given parenthesis strings can be ordered so their concatenation forms a correct bracket sequence.
- Level
Medium6 of 10
- Topics
- Greedy, Sorting, Prefix sum
- Solved
- No attempts yet
Problem
You are given strings , each consisting of ( and ) only. Decide whether the strings can be arranged in some order so that their concatenation is a valid parenthesis string. You may reorder the strings, but the contents of each string stay as they are.
A valid parenthesis string is defined as follows.
- The empty string is valid.
- If and are valid, then the concatenation of and is valid.
- If is valid, then the string obtained by putting inside a pair of matching parentheses is valid.
- No other string is valid.
For example, ()() and (()) are valid, while ()) and ((() are not.
Input
The first line contains the number of strings (). Each of the next lines contains one string (). Every character of is ( or ).
Output
Print Yes on the first line if a valid parenthesis string can be built, and No otherwise. Do not print the quotes.