FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithms-1/array/longest_non_repeat.py at master · githubch/algorithms-1 · GitHub
githubch
/
algorithms-1
Public
forked from
keon/algorithms
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithms-1
/
array
/
longest_non_repeat.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
27 lines (21 loc) · 746 Bytes
Breadcrumbs
algorithms-1
/
array
/
longest_non_repeat.py
Copy path
File metadata and controls
27 lines (21 loc) · 746 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
# Given a string, find the length of the longest substring
# without repeating characters.
# Examples:
# Given "abcabcbb", the answer is "abc", which the length is 3.
# Given "bbbbb", the answer is "b", with the length of 1.
# Given "pwwkew", the answer is "wke", with the length of 3.
# Note that the answer must be a substring,
# "pwke" is a subsequence and not a substring.
def
longest_non_repeat
(
s
):
start
,
maxlen
=
0
,
0
used_char
=
{}
for
i
,
char
in
enumerate
(
s
):
if
char
in
used_char
and
start
<=
used_char
[
char
]:
start
=
used_char
[
char
]
+
1
else
:
maxlen
=
max
(
maxlen
,
i
-
start
+
1
)
used_char
[
char
]
=
i
return
maxlen
a
=
"abcabcdefbb"
print
(
a
)
print
(
longest_non_repeat
(
a
))
Back
|
FazBrowse Home
|
New Git URL