-
Notifications
You must be signed in to change notification settings - Fork 3
/
Demo.py
71 lines (68 loc) · 2.26 KB
/
Demo.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class Solution(object):
# def lengthOfLongestSubstring(self, s):
# """
# :type s: str
# :rtype: int
# """
# sls = len(set(s))
# ls = len(s)
# if ls >= 1:
# max_l = 1
# else:
# return 0
# for i in range(ls):
# for j in range(i + max_l + 1, i + sls + 1):
# curr = s[i:j]
# c_ls = len(curr)
# if len(set(curr)) != c_ls:
# break
# else:
# if c_ls > max_l:
# max_l = c_ls
# if max_l == sls:
# return sls
# return max_l
# def lengthOfLongestSubstring(self, s):
# sls = len(set(s))
# ls = len(s)
# if ls >= 1:
# max_l = 1
# else:
# return 0
# for i in range(ls):
# for j in reversed(range(i + max_l + 1, i + sls + 1)):
# curr = s[i:j]
# c_ls = len(curr)
# if len(set(curr)) == c_ls:
# if c_ls > max_l:
# max_l = c_ls
# if max_l == sls:
# return sls
# break
# return max_l
# def lengthOfLongestSubstring(self, s):
# exist = [False] * 256
# ls = len(s)
# max_len = i = 0
# for j in range(ls):
# while(exist[ord(s[j])]):
# exist[ord(s[i])] = False
# i += 1
# exist[ord(s[j)] = True
# max_len = max(max_len, j - i + 1)
# return max_len
def lengthOfLongestSubstring(self, s):
# https://leetcode.com/articles/longest-substring-without-repeating-characters/
charMap = {}
for i in range(256):
charMap[i] = -1
ls = len(s)
i = max_len = 0
for j in range(ls):
# Note that when charMap[ord(s[j])] >= i, it means that there are
# duplicate character in current i,j. So we need to update i.
if charMap[ord(s[j])] >= i:
i = charMap[ord(s[j])] + 1
charMap[ord(s[j])] = j
max_len = max(max_len, j - i + 1)
return max_len