Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import collections
- def longest_nonrepeating_substring(s):
- r = 0
- c = collections.Counter(s[0])
- best_width = 0
- for l in range(len(s)):
- while r < len(s)-1 and c[s[r+1]] < 1:
- r += 1
- c[s[r]] += 1
- best_width = max(best_width, 1+r-l)
- c[s[l]] -= 1
- return best_width
- test_cases = (
- ("abcabcbb", 3),
- ("bbbbb", 1),
- ("pwwkew", 3)
- )
- for s, expected in test_cases:
- print("Input:", s)
- print("Expected output:", expected)
- actual = longest_nonrepeating_substring(s)
- if actual == expected:
- print(" PASSED.")
- else:
- print(" FAILED. Actual output:", actual)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement