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

3349 단어 leetcodeleetcode

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

좋은 웹페이지 즐겨찾기