File tree Expand file tree Collapse file tree 2 files changed +22
-12
lines changed Expand file tree Collapse file tree 2 files changed +22
-12
lines changed Original file line number Diff line number Diff line change 12
12
13
13
def longest_non_repeat_v1 (string ):
14
14
"""
15
- Finds the length of the longest substring
16
- without repeating characters.
15
+ Apply slide windown.
17
16
"""
18
- if string is None :
19
- return 0
20
- temp = []
21
- max_len = 0
22
- for i in string :
23
- if i in temp :
24
- temp = []
25
- temp .append (i )
26
- max_len = max (max_len , len (temp ))
27
- return max_len
17
+ dict = {}
18
+ max_length = 0
19
+ j = 0
20
+ for i in range (len (string )):
21
+ if string [i ] in dict :
22
+ j = max (dict [string [i ]], j )
23
+ dict [string [i ]] = i + 1
24
+ max_length = max (max_length , i - j + 1 )
25
+ return max_length
28
26
29
27
30
28
def longest_non_repeat_v2 (string ):
Original file line number Diff line number Diff line change @@ -147,6 +147,12 @@ def test_longest_non_repeat_v1(self):
147
147
string = "pwwkew"
148
148
self .assertEqual (longest_non_repeat_v1 (string ), 3 )
149
149
150
+ string = "dvdf"
151
+ self .assertEqual (longest_non_repeat_v1 (string ), 3 )
152
+
153
+ string = "asjrgapa"
154
+ self .assertEqual (longest_non_repeat_v1 (string ), 6 )
155
+
150
156
def test_longest_non_repeat_v2 (self ):
151
157
152
158
string = "abcabcbb"
@@ -158,6 +164,12 @@ def test_longest_non_repeat_v2(self):
158
164
string = "pwwkew"
159
165
self .assertEqual (longest_non_repeat_v2 (string ), 3 )
160
166
167
+ string = "dvdf"
168
+ self .assertEqual (longest_non_repeat_v2 (string ), 3 )
169
+
170
+ string = "asjrgapa"
171
+ self .assertEqual (longest_non_repeat_v2 (string ), 6 )
172
+
161
173
162
174
class TestMaxOnesIndex (unittest .TestCase ):
163
175
You can’t perform that action at this time.
0 commit comments