Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Longest substring without repeating characters in python

$
0
0

This is a pretty standard interview question. Find the longest substring without repeating characters. Here are the test cases,

abcabcbb -> 3bbbbb -> 1pwwkew -> 3bpfbhmipx -> 7tmmzuxt -> 5

Here'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_substring

My 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.


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>