Skip to content

Latest commit

 

History

History
80 lines (51 loc) · 1.87 KB

_1525. Number of Good Ways to Split a String.md

File metadata and controls

80 lines (51 loc) · 1.87 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 01, 2024

Last updated : June 01, 2024


Related Topics : Hash Table, String, Dynamic Programming, Bit Manipulation

Acceptance Rate : 68.325 %


Solutions

Python

# Literally middle of the pact consistently [50, 70%]

class Solution:
    def numSplits(self, s: str) -> int:
        counter = 0

        left, right = {}, dict(Counter(list(s)))

        for i in s :
            left[i] = left.get(i, 0) + 1

            if right.get(i) == 1 :
                right.pop(i)
            else :
                right[i] = right.get(i) - 1

            if len(right.keys()) == len(left.keys()) :
                counter += 1

        return counter
            
# V2 is a bit faster fluctuating around the 75% mark

class Solution:
    def numSplits(self, s: str) -> int:
        counter = 0

        left, right = set(), set()
        lefty, righty = [0] * len(s), [0] * len(s)
        
        for i in range(0, len(s)) :
            left.add(s[i])
            right.add(s[len(s) - i - 1])
            
            lefty[i] = len(left)
            righty[len(s) - i - 1] = len(right)
        
        for i in range(len(s) - 1) :
            if lefty[i] == righty[i + 1] :
                counter += 1

        return counter