leetcode395:至少有K个重复字符的最长子字符串

Example:

Input:
s = “ababbc”, k = 2
Output:
5
The longest substring is “ababb”, as ‘a’ is repeated 2 times and ‘b’ is repeated 3 times.

code:

class Solution:
    def longestSubstring(self, s, k):
        """
        :type s: str
        :type k: int
        :rtype: int
        """
        for i in set(s):
            if s.count(i) < k: # 找出不满足k次的字母
                return max(self.longestSubstring(m, k) for m in s.split(i)) # 将其作为分割点进行分治
        return len(s)

你可能感兴趣的:(python)