-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob_3.py
31 lines (27 loc) · 890 Bytes
/
prob_3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 3. Longest Substring Without Repeating Characters
# https://leetcode.com/problems/longest-substring-without-repeating-characters/
# instead of taking max to change the value for j and max_len, it is better just use if .
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
# left pointers
if len(s) == 0:
return 0
j = 0
N = len(s)
max_len = 1
last_pos_dict = {s[0]: 0}
for i in range(1, N):
ch = s[i]
if ch in last_pos_dict:
last_pos = last_pos_dict[ch] + 1
if last_pos > j:
j = last_pos
last_pos_dict[ch] = i
cur_len = i - j + 1
if cur_len > max_len:
max_len = cur_len
return max_len