File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed
Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change 1+ # "균형잡힌 괄호 문자열"의 인덱스 반환
2+ def balanced_index (p ):
3+ count = 0 # 왼쪽 괄호의 개수
4+ for i in range (len (p )):
5+ if p [i ] == '(' :
6+ count += 1
7+ else :
8+ count -= 1
9+ if count == 0 :
10+ return i
11+
12+ # "올바른 괄호 문자열"인지 판단
13+ def check_proper (p ):
14+ count = 0 # 왼쪽 괄호의 개수
15+ for i in p :
16+ if i == '(' :
17+ count += 1
18+ else :
19+ if count == 0 : # 쌍이 안 맞으면
20+ return False
21+ count -= 1
22+ return True
23+
24+ def solution (p ):
25+ answer = ''
26+ if p == '' :
27+ return answer
28+ index = balanced_index (p )
29+ u = p [:index + 1 ]
30+ v = p [index + 1 :]
31+ # "올바른 괄호 문자열"이면, v에 대해 함수를 수행한 결과를 붙여 반환
32+ if check_proper (u ):
33+ answer = u + solution (v )
34+ # "올바른 괄호 문자열"이 아니라면 아래의 과정을 수행
35+ else :
36+ answer = '('
37+ answer += solution (v )
38+ answer += ')'
39+ u = list (u [1 :- 1 ]) # 첫 번째와 마지막 문자를 제거
40+ for i in range (len (u )):
41+ if u [i ] == '(' :
42+ u [i ] = ')'
43+ else :
44+ u [i ] = '('
45+ answer += "" .join (u )
46+ return answer
You can’t perform that action at this time.
0 commit comments