def solution(absolutes, signs):
answer = 0
for i in range(len(signs)):
if signs[i]:
answer += absolutes[i]
else:
answer -= absolutes[i]
return answer
쉬운 문제였다.
def roll(s):
s = s[1:] + s[0]
print(s)
return s
def isTrue(s):
res = [0] * 3
for i in s:
if i == '(':
res[0] += 1
elif i == ')':
res[0] -= 1
if res[0] < 0:
return False
elif i == '{':
res[1] += 1
elif i == '}':
res[1] -= 1
if res[1] < 0:
return False
elif i == '[':
res[2] += 1
elif i == ']':
res[2] -= 1
if res[2] < 0:
return False
if sum(res) == 0:
return True
else:
return False
def solution(s):
answer = 0
count = 0
while count < len(s):
count += 1
if isTrue(s):
answer += 1
s = roll(s)
return answer
풀긴했지만 반복되는 부분이 많아서 코드가 복잡했다.
그래서 해설을 보고 stack을 이용해서 다시 풀어보았다.
op = ['(', '{', '[']
cl = [')', '}', ']']
def roll(s):
s = s[1:] + s[0]
return s
def isTrue(s):
st = []
flag = True
for i in s:
if i in op:
st.append(i)
else:
if len(st) > 0 and op[cl.index(i)] == st[-1]:
st.pop()
else:
flag = False
break
if flag and len(st) == 0:
return True
else:
return False
def solution(s):
answer = 0
for _ in range(len(s)):
if isTrue(s):
answer += 1
s = roll(s)
return answer