Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def longest_substring(word):
- longest = current = 0
- seen = {}
- for i in range(len(word)):
- character = word[i]
- if character in seen:
- longest = max(longest, (i - seen[character]))
- current = 0
- current += 1
- seen[word[i]] = i
- return max(longest, current)
- assert longest_substring('abcabcbb') == 3
- assert longest_substring('pwwkew') == 3
- assert longest_substring('abcdefgh') == 8
- assert longest_substring('abadefgh') == 6
- assert longest_substring('aaaaa') == 1
- assert longest_substring('') == 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement