A valid parentheses string is either empty (""), "(" + A + ")", or A + B, where A and B are valid parentheses strings, and + represents string concatenation. For example, "", "()", "(())()", and "(()(()))" are all valid parentheses strings.
A valid parentheses string S is primitive if it is nonempty, and there does not exist a way to split it into S = A+B, with A and B nonempty valid parentheses strings.
Given a valid parentheses string S, consider its primitive decomposition: S = P_1 + P_2 + ... + P_k, where P_i are primitive valid parentheses strings.
Return S after removing the outermost parentheses of every primitive string in the primitive decomposition of S.
class Solution:
def removeOuterParentheses(self, S: str) -> str:
temp = ""
result = ""
isOuter = False
count = 0
for i in range(len(S)):
# 시작 괄호는 isOuter
if not isOuter and S[i] == "(":
isOuter = True
count = 0
continue
# Outer 가 있는 상태면 모두 temp 에 넣어주기
if isOuter:
if S[i] == "(":
temp += "("
count += 1
else:
# count 가 0 인데 ")" 를 또 만나면 Outer 짝꿍이니까 result 에 update
if count == 0:
result += temp
isOuter = False
temp = ""
else:
temp += ")"
count -= 1
return result
바깥 괄호를 만나면 isOuter = True
=> 짝꿍 )
를 만날 때까지 temp
에 넣어주면서 괄호 count
if count == 0:
인데 )
를 만났으면 아까 못 찾은 짝꿍이니까 result
에 update
어려운게 아닌데 머리가 안돌아가서 좀 걸렸네요;
Alice and Bob take turns playing a game, with Alice starting first.
Initially, there is a number N on the chalkboard. On each player's turn, that player makes a move consisting of:
Also, if a player cannot make a move, they lose the game.
Return True if and only if Alice wins the game, assuming both players play optimally.
class Solution:
def divisorGame(self, N: int) -> bool:
# 0 이면 Bob 우승 => False, 1 이면 Alice 우승 => True
winner = 0
if N == 1:
return False
i = N-1
while N != i and N and i != 0:
if N % i == 0:
N = N - i
winner = not winner
else:
i -= 1
if winner:
return True
return False
while
문 돌려서 N
의 약수를 찾으면 winner
바꿔주고 N
은 N - 약수
로 update
최종적으로 winner
가 0 이면 Bob 우승 => False
, 1 이면 Alice 우승 => True