|
12 | 12 |
|
13 | 13 | class Solution:
|
14 | 14 | def lengthOfLongestSubstring(self, s):
|
15 | | - i, cnt = 0, 0 |
16 | | - HashMap = {} |
17 | | - for idx, val in enumerate(s): |
18 | | - if HashMap.get(val): i = max(HashMap.get(val), i) |
19 | | - HashMap[val] = idx + 1 |
20 | | - cnt = max(cnt, idx-i+1) |
21 | | - return cnt |
| 15 | + # i, cnt = 0, 0 |
| 16 | + # HashMap = {} |
| 17 | + # for idx, val in enumerate(s): |
| 18 | + # if HashMap.get(val): i = max(HashMap.get(val), i) |
| 19 | + # HashMap[val] = idx + 1 |
| 20 | + # cnt = max(cnt, idx-i+1) |
| 21 | + # return cnt |
22 | 22 |
|
23 | | -s = 'abcabcbb' |
24 | | -s = 'pwwkew' |
25 | | -s = 'ewwkea' |
| 23 | + res = left = right = 0 |
| 24 | + hashMap = {} |
| 25 | + while right < len(s): |
| 26 | + if s[right] in hashMap: |
| 27 | + res = max(res, right-left) |
| 28 | + left = max(left, hashMap[s[right]]) |
| 29 | + hashMap[s[right]] = right+1 |
| 30 | + right += 1 |
| 31 | + res = max(res, right-left) |
| 32 | + return res |
| 33 | + |
| 34 | +s = "abcabcbb" |
| 35 | +# s = "bbbbb" |
| 36 | +# s = "pwwkew" |
| 37 | +# s = "" |
| 38 | +# s = " " |
| 39 | +# s = "ab" |
| 40 | +# s = "abba" |
26 | 41 |
|
27 | 42 | mat = Solution()
|
28 | 43 | mat.lengthOfLongestSubstring(s)
|
0 commit comments