-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestSubstringWithoutRepeatingChar.py
More file actions
91 lines (85 loc) · 2.11 KB
/
Copy pathLongestSubstringWithoutRepeatingChar.py
File metadata and controls
91 lines (85 loc) · 2.11 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<<<<<<< HEAD
#more efficient
def lengthOfLongestSubstring(s):
if len(s)==0:
return 0
dic = { }
start = 0
maxx = 1
b = True
for i in range(len(s)):
if s[i] not in dic:
b = True
dic[s[i]] = i
elif dic[s[i]]<start:
b = True
dic[s[i]] = i
else:
b = False
maxx = max(maxx, i-start)
start = dic[s[i]]+1
dic[s[i]] = i
if b:
maxx = max(maxx, i-start+1)
return maxx
def l2(s):
dic = {s[0]:0}
count, maxx = 1,1
i = 1
while i < len(s):
if s[i] not in dic:
count += 1
dic[s[i]] = i
i += 1
else:
i = dic[s[i]]+1
dic.clear()
count = 0
maxx = max(maxx, count)
return (maxx)
string = "au"
string2= "wejzbcmkkblrnktzqeugtjsrlajlvhsrldqmfeyrhkjwtmbncvfmxvedtkpvggwrtwtwt"
print(lengthOfLongestSubstring(string2),l2(string2))
=======
#more efficient
def lengthOfLongestSubstring(s):
if len(s)==0:
return 0
dic = { }
start = 0
maxx = 1
b = True
for i in range(len(s)):
if s[i] not in dic:
b = True
dic[s[i]] = i
elif dic[s[i]]<start:
b = True
dic[s[i]] = i
else:
b = False
maxx = max(maxx, i-start)
start = dic[s[i]]+1
dic[s[i]] = i
if b:
maxx = max(maxx, i-start+1)
return maxx
def l2(s):
dic = {s[0]:0}
count, maxx = 1,1
i = 1
while i < len(s):
if s[i] not in dic:
count += 1
dic[s[i]] = i
i += 1
else:
i = dic[s[i]]+1
dic.clear()
count = 0
maxx = max(maxx, count)
return (maxx)
string = "au"
string2= "wejzbcmkkblrnktzqeugtjsrlajlvhsrldqmfeyrhkjwtmbncvfmxvedtkpvggwrtwtwt"
print(lengthOfLongestSubstring(string2),l2(string2))
>>>>>>> 7435d7724d8f64fc9efc8a6ea58c5cca383a1284