python solution beats 95%


  • 0
    M

    The idea is simple. Find all characters that their frequency in the string is less than k and put it in bad_chars array. Then break the string when we encounter a character in bad_chars and recursively call the longestSubstring.

    if len(s) < k:
                return 0
            if s == "":
                return 0
    
            dic = {}
    
            for i in s:
                dic[i] = dic.get(i,0) + 1
    
            bad_chars = []
            for c, v in dic.items():
                if v < k:
                    bad_chars.append(c)
            if len(bad_chars) == 0:
                return len(s)
    
            max_s = 0
            start = 0
            for i in range(len(s)):
                if s[i] in bad_chars:
                    max_s = max(max_s, self.longestSubstring(s[start:i], k))
                    start = i+1
    
            max_s = max(max_s, self.longestSubstring(s[start:], k))
            return max_s
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.