Python solution using two Counters


  • 0
    D
    def longestSubstring(self, s, k):
    	c1 = Counter(s)
    	total = [c1[x] >= k for x in c1]
    	if all(total): return len(s)
    	if not any(total): return 0
    	
    	c2 = Counter()
    	ans, start, end = 0, 0, 0
    	
    	for char in s:
    		end += 1
    		if c1[char] >= k:
    			c2[char] += 1
    		else:
    			ans = max(ans, self.longestSubstring(s[start:end-1], k))
    			c1 -= c2
    			c2 = Counter()
    			start = end
    	ans = max(ans, self.longestSubstring(s[start:end], k))
    	
    	return ans

Log in to reply
 

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