Leetcode 2405. Optimal Partition of String [Python]

贪心。设置temp string,遇到新的char在temp中没出现过,就加入到temp,直到遇到重复的char,将temp加入到res中,将temp归零到空的string。

class Solution:
    def partitionString(self, s: str) -> int:
        N = len(s)
        j = 0
        res = []
        while j < N:
            temp = ''
            while j < N and s[j] not in temp:
                temp += s[j]
                j += 1
            res.append(temp) 
        #print(res)
        return len(res)

你可能感兴趣的:(Leetcode学习记录,leetcode,算法,职场和发展)