Algorithm/NeetCode

Optimal Partition of String

Tony Lim 2024. 11. 14. 13:46
728x90

https://leetcode.com/problems/optimal-partition-of-string/description/

class Solution:
    def partitionString(self, s: str) -> int:
        curSet = set()
        res = 1

        for c in s:
            if c in curSet:
                res += 1
                curSet = set()
                curSet.add(c)
            else:
                curSet.add(c)
        return res

use HashSet to check if substring has duplicated alphabet, 

result is default to 1 because given s string will be always non empty

 

728x90

'Algorithm > NeetCode' 카테고리의 다른 글

Minimum Penalty for a Shop  (0) 2024.11.17
Design Underground System  (0) 2024.11.15
Number of Zero-Filled Subarrays  (0) 2024.11.13
First Missing Positive  (1) 2024.11.12
Non-decreasing Array  (2) 2024.11.11