Leetcode # 1525 (Python): Number of Good Ways to Split a String

정욱·2021년 5월 1일
0

Leetcode

목록 보기
38/38

Number of Good Ways to Split a String

  • Difficulty: Medium
  • Type: Stack/Que, String
  • link

Problem

Solution

  • Time Complexity: O(n)
import collections
class Solution:
    def numSplits(self, s: str) -> int:
        count = 0
        left = set()
        right = set(s)
        deq = collections.deque(s)
        for _ in range(len(s)-1):
            popped = deq.popleft()
            left.add(popped)
            if popped not in deq:
                right.remove(popped)
            if len(left) == len(right):
                count += 1
        return count

0개의 댓글