
처음에는 .replace의 방법으로 풀었는데 너무 시간이 오래걸렸다.
도저히 무슨 방법을 써야하지 답이 생각이 안났는데, stack 방법을 찾게 되어서, stack을 이용해서 새로 풀었다.
효율성 검사에서 시간이 걸려, 조마조마했지만 다행스럽게 통과되었다.
stack을 이용하되, pop()과 append()를 사용하게 되면 시간이 더 오래 걸릴 것이라 판단해서 top을 이용해서 문제를 풀었다.
다행히 몇 번의 반복문 없이 for문 한 번만 돌리면 문제가 해결되어서 더 골머리 앓지 않아도 되어서 다행이다.
# stack 이용
def solution(s):
stack = [0] * 1000001
# top을 이용해서 시간 줄이기
top = -1
for i in range(len(s)):
if stack[top] != s[i]:
top += 1
stack[top] = s[i]
else:
top -= 1
return 1 if top == -1 else 0
동일한 방법이지만, top을 사용하지 않은 방법을 썼다.
def solution(s):
stack = []
for i in list(s):
if (not stack) or stack[-1]!=i: stack.append(i)
else: stack.pop()
return 0 if stack else 1
그리고 아래는 지금은 안되는 방법이라고 하지만 뭐.. 보아하니 이것도 stack을 사용한 방법이니 가져와봤다.
def solution(s):
answer = []
for i in s:
if not(answer):
answer.append(i)
else:
if(answer[-1] == i):
answer.pop()
else:
answer.append(i)
return not(answer)
```def solution(s):
answer = []
for i in s:
if not(answer):
answer.append(i)
else:
if(answer[-1] == i):
answer.pop()
else:
answer.append(i)
return not(answer)