Back to course home
0% completed
Vote For New Content
Dear authors...Why are the sample solutions so unwieldly hard to read?
Luis Roel
Dec 10, 2024
def longestSubstring(str1, k): l = 0 max_len = 0 counts = {} for r in range(len(str1)): # Add in an item to the window, by adding 1 to its count counts[str1[r]] += counts.get(str1[r], 0) + 1 # Now we contract the window if we've seen more than # 'k' characters while len(counts) > k: # Take an item out of the window by subtracting # 1 from its ounts counts[str1[l]] -= 1 # If it turns out that we've exhausted this key, # delete it from the dict if counts[str1[l]] == 0: del counts[str1[l]] # Then shift our pointer to the right l += 1 max_len = max(max_len, r - l + 1) return max_len
0
0
Comments
Comments
On this page