This is a pretty standard interview question. Find the longest substring without repeating characters. Here are the test cases,
abcabcbb -> 3bbbbb -> 1pwwkew -> 3bpfbhmipx -> 7tmmzuxt -> 5Here's my code which uses a pretty simple approach with 2 pointers.
def lengthOfLongestSubstring(s): checklist = {} starting_index_of_current_substring = 0 length_of_longest_substring = 0 for i, v in enumerate(s): if v in checklist: starting_index_of_current_substring = checklist[v] + 1 else: length_of_current_substring = i - starting_index_of_current_substring + 1 length_of_longest_substring = max(length_of_current_substring, length_of_longest_substring) checklist[v] = i return length_of_longest_substringMy code passes all the test cases except the last one (actual 4, expected 5). Can someone help me modify the code to take care of the last test case. I don't wish to reinvent the algorithm.