3 lines Python O(n*log(n))

  • 0
    def longestSubstring(self, s, k):
        split = ''.join([key for key,v in Counter(s).items() if v < k])
        if(not split): return len(s)        
        return max([self.longestSubstring(sub, k) for sub in re.split('[' + split + ']+', s)])

    Long version:

    def longestSubstring(self, s, k):
        chars = [key for key,v in Counter(s).items() if v < k]
        split = ''.join(chars)
        if(not split):
            return len(s)
        chars_split = re.split('[' + split + ']+', s)
        longest = 0
        for sub in chars_split:
            longest = max(longest, self.longestSubstring(sub, k))
        return longest

Log in to reply

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