괄호 회전하기
다음 규칙을 지키는 문자열을 올바른 괄호 문자열이라고 정의합니다.
(), [], {} 는 모두 올바른 괄호 문자열입니다.
만약 A가 올바른 괄호 문자열이라면, (A), [A], {A} 도 올바른 괄호 문자열입니다. 예를 들어, [] 가 올바른 괄호 문자열이므로, ([]) 도 올바른 괄호 문자열입니다.
만약 A, B가 올바른 괄호 문자열이라면, AB 도 올바른 괄호 문자열입니다. 예를 들어, {} 와 ([]) 가 올바른 괄호 문자열이므로, {}([]) 도 올바른 괄호 문자열입니다.
대괄호, 중괄호, 그리고 소괄호로 이루어진 문자열 s가 매개변수로 주어집니다. 이 s를 왼쪽으로 x (0 ≤ x < (s의 길이)) 칸만큼 회전시켰을 때 s가 올바른 괄호 문자열이 되게 하는 x의 개수를 return 하도록 solution 함수를 완성해주세요.
s의 길이는 1 이상 1,000 이하입니다.
s | result |
---|---|
"["]"(")"{}" | 3 |
"}]()[{" | 2 |
"[)(]" | 0 |
"}}}" | 0 |
from collections import deque
def solution(s):
s = deque(s)
answer = -1
result = 0
while len(s) > 0:
if s[0] == '(' or s[0] == '{' or s[0] == '[':
result += 1
s.popleft()
else:
s.popleft()
print(result)
return result
def checking(a):
stack = []
for i in a: # 라스트팡처럼 짝이 맞으면 pop
if (i == '(') or (i == '{') or (i == '['):
stack.append(i)
elif stack and i == ')' and stack[-1] == '(':
stack.pop()
elif stack and i == '}' and stack[-1] == '{':
stack.pop()
elif stack and i == ']' and stack[-1] == '[':
stack.pop()
else:
return 0
if len(stack) == 0:
return 1
else:
return 0
def solution(s):
arr = list(s)
result = 0
cnt = 0
temp = 0
while cnt != len(arr):
cnt += 1
temp = arr[0]
for i in range(len(arr)-1):
arr[i] = arr[i+1]
arr[-1] = temp
print("arr", arr)
if checking(arr) == 1:
result += 1
return result
감사합니다. 이런 정보를 나눠주셔서 좋아요.