Longest Substring With K Unique Characters
Added 2020-12-19 14:48:05 +0000 UTCVideo will be uploaded tomorrow evening, due to some technical difficulty cant upload now.
Comments
class Solution: def lengthOfLongestSubstringKDistinct(self, s: str, k: int) -> int: i=j=0 global_longest=0 tracker={} a=list(s) while j < len(a): curr_tracker_len=len(tracker) if curr_tracker_len < k: if a[j] in tracker.keys(): tracker[a[j]]=tracker[a[j]] + 1 else: tracker[a[j]]=1 elif curr_tracker_len > k: while len(tracker) > k: counter= tracker[a[i]]-1 if counter==0: del tracker[a[i]] else: tracker[a[i]]=counter i+=1 else: global_longest=max(global_longest,j-i+1) j+=1 return global_longest https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/ it's not working for "eceba" input
aarjay singh
2024-01-17 00:56:40 +0000 UTCplease share the java code as well.
Seema Kumari
2022-05-21 16:16:34 +0000 UTC