Advertisement
Guest User

Untitled

a guest
May 26th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. def longest_substring(word):
  2. longest = current = 0
  3. seen = {}
  4. for i in range(len(word)):
  5. character = word[i]
  6. if character in seen:
  7. longest = max(longest, (i - seen[character]))
  8. current = 0
  9. current += 1
  10. seen[word[i]] = i
  11. return max(longest, current)
  12.  
  13.  
  14. assert longest_substring('abcabcbb') == 3
  15. assert longest_substring('pwwkew') == 3
  16. assert longest_substring('abcdefgh') == 8
  17. assert longest_substring('abadefgh') == 6
  18. assert longest_substring('aaaaa') == 1
  19. assert longest_substring('') == 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement