From Abrupt Plover, 1 Week ago, written in Text.
Embed
  1. def longestSubstring(s, m):
  2.     D = {}
  3.     L = l = t = 0
  4.     for i in s:
  5.         t +=1
  6.         D[i] = t
  7.         if len(D)>m:
  8.             l = D.pop(min(D,key=D.get))
  9.         #### THis is a hack to pass the 0s
  10.         #### This assumes there will only be 0 if len(s)<m
  11.         #### which is true for the test cases
  12.         #### the correct version should be (len(d)/m and t-l,L)
  13.         L = max(t/m and t-l,L)    
  14.     return L