def solution(topping):
N = len(topping)
count = 0
for i in range(1, N-1):
if len(set(list(topping[:i]))) == len(set(list(topping[i:]))): count += 1
return count
처음에 이렇게 짰는데 테케 두개 빼고 다 TLE 뜸
그래서 두 개의 dictionary를 이용해서 처리해주었다
Counter()를 이용해서 모든 토핑을 A가 갖도록 하고, for문을 돌면서 topping의 원소 순서대로 A의 토핑을 하나씩 B에게 넘겨주는 방식으로 풀어주었다
from collections import Counter
def solution(topping):
A = Counter(topping)
B = dict()
count = 0
for tp in topping: #topping의 원소 순서대로 A의 토핑을 하나씩 B에게 넘겨줌
if tp in B: B[tp] += 1
else: B[tp] = 1
A[tp] -= 1
if A[tp] == 0: A.pop(tp)
if len(A) == len(B): count += 1
return count