Skip to content

Latest commit

 

History

History
81 lines (61 loc) · 2.19 KB

_2405. Optimal Partition of String.md

File metadata and controls

81 lines (61 loc) · 2.19 KB

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

Back to top


First completed : June 23, 2024

Last updated : June 23, 2024


Related Topics : Hash Table, String, Greedy

Acceptance Rate : 77.992 %


Solutions

Python

# Wanted to see if a 26-long list being reset each time would be faster
# than a hashmap

class Solution:
    def partitionString(self, s: str) -> int:
        current: List[bool] = [False] * 26

        counter = 1
        for c in s :
            if current[ord(c) - ord('a')] :
                counter += 1
                current = [False] * 26
                current[ord(c) - ord('a')] = True
            
            else :
                current[ord(c) - ord('a')] = True

        return counter
class Solution:
    def partitionString(self, s: str) -> int:
        lastCase: List[int] = [-1] * 26

        counter = 1
        leftPointer = 0
        for i, c in enumerate(s) :
            if lastCase[ord(c) - ord('a')] >= leftPointer :
                leftPointer = i
                counter += 1
            lastCase[ord(c) - ord('a')] = i

        return counter
class Solution:
    def partitionString(self, s: str) -> int:
        current = set()

        counter = 1
        for c in s :
            if c in current :
                counter += 1
                current = set()
            current.add(c)

        return counter