PARENTHESIS SEQUENCE Solutions BVPCSI03

Page Contents

PARENTHESIS SEQUENCE Solutions BVPCSI03

‘(‘ and ‘)′ are known as enclosure. An arrangement containing bracket is given however it’s not in a request that you like (or perhaps it is). To fix the given arrangement you can move all things considered one bracket from its unique spot in the succession to some other position. You can’t change over an open bracket into a shut one for example you can’t switch a section. We should call the arrangement you need a decent grouping.

A decent arrangement (s) can be: s is vacant s is equivalent to “(t)”, where t is a decent arrangement. s is equivalent to t1.t2, for example connection of t1 and t2, where t1 and t2 are acceptable successions.

Forexample, “(()())”,”()” are right, while “)(” and “())” are definitely not.

Attempt to fix the aftereffect and comprehend whether you can move one section so the succession gets right or not.

Info First of line of info contains a solitary number n — length of the arrangement.

Second line of the info contains section arrangement of length n, containing images “(” and “)”.

Yield Print “Yes” on the off chance that you can make the arrangement right moving all things considered one section. In any case print “No”.

Limitations

(1<=n<=200000)

Test input

)(

Test yield

Truly

Test input

(()

Test yield

No

Test input

()

Test yield

Truly

Test input

10

)))))(((((

Test yield

No

Clarification:

In the main model, you can move first section as far as possible, consequently transforming the succession into “()”, which is right section grouping.

In the subsequent model, it is highly unlikely to move all things considered one section with the goal that the arrangement gets right.

In the third model, the succession is as of now right and there’s no compelling reason to move sections.

CODE PYTHON 3.6 :

n = int(raw_input())
b = list(raw_input())[:n]
c = []
if len(b) % 2 == 0:
for e in b:
if c:
if c[-1] == ‘(‘ and e == ‘)’:
c = c[:-1]
continue
c.append(e)
if len(c) == 2 and not c[0] == c[1]:
print(‘Yes’)
elif len(c) == 0:
print(‘Yes’)
else:
print(‘No’)
else:
print(‘No’)