Last active
February 9, 2018 16:32
-
-
Save senthil1216/98de2b3431159f9073c44f73bdadd886 to your computer and use it in GitHub Desktop.
LongestSubstring without repeating characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution(object): | |
| def lengthOfLongestSubstring(self, s): | |
| if not s or len(s) == 0: return 0 | |
| store = {} | |
| max_len, prevIdx, idx, currChar = 0, 0, 0, '' | |
| while idx < len(s): | |
| currChar = s[idx] | |
| if currChar not in store: | |
| store[currChar] = idx | |
| else: | |
| curr_len = idx-prevIdx | |
| max_len = max(max_len, curr_len) | |
| currIdx = store[currChar] | |
| prevIdx = max(prevIdx, currIdx+1) | |
| store[currChar] = idx | |
| idx += 1 | |
| max_len = max(max_len, idx-prevIdx) | |
| return max_len |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment