From Chocolate Owl, 1 Week ago, written in Text.
Embed
  1. def longestSubstring(s, k):
  2.     D = {}
  3.     m = a = i = l = 0
  4.     for c in s:
  5.         D[c] = t = D.get(c,0) + 1
  6.         l += t < 2
  7.         if l > k:
  8.             D[s[a]] -= 1
  9.             l -= D[s[a]] < 1
  10.             a += 1
  11.         i += 1
  12.         m = max(m, i-a)
  13.     return [m,0][m<k]