30 lines
1002 B
Python
30 lines
1002 B
Python
def match_parens(lst):
|
|
'''
|
|
You are given a list of two strings, both strings consist of open
|
|
parentheses '(' or close parentheses ')' only.
|
|
Your job is to check if it is possible to concatenate the two strings in
|
|
some order, that the resulting string will be good.
|
|
A string S is considered to be good if and only if all parentheses in S
|
|
are balanced. For example: the string '(())()' is good, while the string
|
|
'())' is not.
|
|
Return 'Yes' if there's a way to make a good string, and return 'No' otherwise.
|
|
|
|
Examples:
|
|
match_parens(['()(', ')']) == 'Yes'
|
|
match_parens([')', ')']) == 'No'
|
|
'''
|
|
def is_balanced(s):
|
|
count = 0
|
|
for c in s:
|
|
if c == '(':
|
|
count += 1
|
|
else:
|
|
count -= 1
|
|
if count < 0:
|
|
return False
|
|
return count == 0
|
|
|
|
s1, s2 = lst[0], lst[1]
|
|
if is_balanced(s1 + s2) or is_balanced(s2 + s1):
|
|
return 'Yes'
|
|
return 'No' |