We define a valid bracket sequence as a string that is either:
- The empty string;
- A string
(B)
, whereB
is a valid bracket sequence. LR
, the concatenation of two stringsL
andR
which are both valid bracket sequences.
Let
(
and)
; , or the subsequence is a valid bracket sequence.
Let
has the same length as ;- for any pair of indices
and , if and are matching brackets, then .
For a given string -1
if no such bracket sequence exists.
Input
The input contains a string
Output
In the output you should write either a string -1
if no such bracket
sequence exists.
Notes and constraints
- For test cases worth
points . - For test cases worth another
points . - We say that a bracket sequence
is lexicographically smaller than a bracket sequence if there is an index , such that for each , and . - Character
(
is considered lexicographically smaller than character)
.
Sample Input 1
abbaaa
Sample Output 1
(()())
Note for Sample Input 1
Another valid bracket sequence is (())()
, but this is not the smallest lexicographic solution.
Sample Input 2
abab
Sample Output 2
-1
Note for Sample Input 2
There is no valid bracket sequence that matches the given string.
Comments