âLongest Substring Without Repeating Charactersâ in Python
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
3
down vote
favorite
I was trying to solve this problem from Leetcode:
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.
I would like to get some code review for the following solution:
class Solution:
def lengthOfLongestSubstring(self, s):
dict =
start, end, curr_length, max_length = 0, 0, 0, 0
while ( end < len(s)):
char = s[end]
if char not in dict:
curr_length += 1
else:
start = max(dict[char], start)
max_length = max(max_length, end - start + 1)
dict[char] = end + 1
end += 1
return max_length
"""
my logic is that
* 1) Initialize a hashmap/dictionary/object that will map characters to their index.
* 2) Initialize `start`, `end`, and `answer`
* 3) Loop through the string while `end` is less than `length` of string
* 4) Check if character at `end` is in hashmap
* a) If so, repeating character exists between `start` and `end`. Set `start` to be the `max_length` between the value of character in the hashmap, and `start`
"""
And it passes all of the leetcode tests.
python
add a comment |Â
up vote
3
down vote
favorite
I was trying to solve this problem from Leetcode:
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.
I would like to get some code review for the following solution:
class Solution:
def lengthOfLongestSubstring(self, s):
dict =
start, end, curr_length, max_length = 0, 0, 0, 0
while ( end < len(s)):
char = s[end]
if char not in dict:
curr_length += 1
else:
start = max(dict[char], start)
max_length = max(max_length, end - start + 1)
dict[char] = end + 1
end += 1
return max_length
"""
my logic is that
* 1) Initialize a hashmap/dictionary/object that will map characters to their index.
* 2) Initialize `start`, `end`, and `answer`
* 3) Loop through the string while `end` is less than `length` of string
* 4) Check if character at `end` is in hashmap
* a) If so, repeating character exists between `start` and `end`. Set `start` to be the `max_length` between the value of character in the hashmap, and `start`
"""
And it passes all of the leetcode tests.
python
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I was trying to solve this problem from Leetcode:
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.
I would like to get some code review for the following solution:
class Solution:
def lengthOfLongestSubstring(self, s):
dict =
start, end, curr_length, max_length = 0, 0, 0, 0
while ( end < len(s)):
char = s[end]
if char not in dict:
curr_length += 1
else:
start = max(dict[char], start)
max_length = max(max_length, end - start + 1)
dict[char] = end + 1
end += 1
return max_length
"""
my logic is that
* 1) Initialize a hashmap/dictionary/object that will map characters to their index.
* 2) Initialize `start`, `end`, and `answer`
* 3) Loop through the string while `end` is less than `length` of string
* 4) Check if character at `end` is in hashmap
* a) If so, repeating character exists between `start` and `end`. Set `start` to be the `max_length` between the value of character in the hashmap, and `start`
"""
And it passes all of the leetcode tests.
python
I was trying to solve this problem from Leetcode:
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.
I would like to get some code review for the following solution:
class Solution:
def lengthOfLongestSubstring(self, s):
dict =
start, end, curr_length, max_length = 0, 0, 0, 0
while ( end < len(s)):
char = s[end]
if char not in dict:
curr_length += 1
else:
start = max(dict[char], start)
max_length = max(max_length, end - start + 1)
dict[char] = end + 1
end += 1
return max_length
"""
my logic is that
* 1) Initialize a hashmap/dictionary/object that will map characters to their index.
* 2) Initialize `start`, `end`, and `answer`
* 3) Loop through the string while `end` is less than `length` of string
* 4) Check if character at `end` is in hashmap
* a) If so, repeating character exists between `start` and `end`. Set `start` to be the `max_length` between the value of character in the hashmap, and `start`
"""
And it passes all of the leetcode tests.
python
edited Aug 12 at 5:38
Jamalâ¦
30.1k11114225
30.1k11114225
asked Aug 11 at 22:12
NinjaG
756221
756221
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
7
down vote
Nice solution, please find a few thoughts below:
Do you need to wrap it in class for leetcode? It's unnecessary otherwise.
Function names use
snake_case
and notcamelCase
, check PEP8 for more details. Hence it would belength_of_longest_substring
.Do not use
dict
as a variable name, it is already built-in to construct dictionaries.I think a for-loop would be preferable over a while-loop in this case, it's more straightforward, less clunky, and easier to understand.
You update
curr_length
, but never actually use it to determine yourmax_length
.The docstring should come right after the
class Solution
and before the first function, and no line should be longer than 79 characters. Moreover, you seem to not have pasted the entire docstrings, it just ends mid-sentence.
add a comment |Â
up vote
3
down vote
Minor technical issues
There are a couple of minor technical issues with the solution:
When looping over an iterable, if you need both the index and the value of the current item, it's good to use
enumerate
, so that you cannot accidentally forget to increment the loop counter, and syntax is nicely compact yet readable.curr_length
is modified but never used: should be removed.The code is most readable when there is one statement per line. So I recommend to split multi-assignments like
start, end, curr_length, max_length = 0, 0, 0, 0
. Simple multi-assignments with 2 terms are usually ok though, for exampleleft, right = ...
when implementing a binary searchThe parentheses are unnecessary around the condition in
while end < len(s):
It's unfortunate use names that are types in many languages, such as
dict
andchar
PEP8 recommends to indent with 4 spaces. And it strongly recommends to use the same indent width consistently. (The posted code indents the class body by 4 spaces, but everything else with 2.)
Alternative implementation
Putting the above tips together, and throwing in some doctests:
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
>>> Solution().lengthOfLongestSubstring("bpfbhmipx")
7
>>> Solution().lengthOfLongestSubstring("abcabcbb")
3
>>> Solution().lengthOfLongestSubstring("bbbbb")
1
>>> Solution().lengthOfLongestSubstring("pwwkew")
3
>>> Solution().lengthOfLongestSubstring("tmmzuxt")
5
>>> Solution().lengthOfLongestSubstring("a")
1
>>> Solution().lengthOfLongestSubstring("")
0
>>> Solution().lengthOfLongestSubstring("abc")
3
"""
longest = 0
start = -1
last =
for i, c in enumerate(s):
if c in last:
start = max(start, last[c])
longest = max(longest, i - start)
last[c] = i
return longest
add a comment |Â
up vote
2
down vote
just a note, for string iteration, a for loop might be more suitable
from
while ( end < len(s)):
char = s[end]
...
end += 1
to
for end, char in enumerate(s)
...
@Mathias Ettinger edited
â Abdur-Rahmaan Janhangeer
Aug 12 at 8:51
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
Nice solution, please find a few thoughts below:
Do you need to wrap it in class for leetcode? It's unnecessary otherwise.
Function names use
snake_case
and notcamelCase
, check PEP8 for more details. Hence it would belength_of_longest_substring
.Do not use
dict
as a variable name, it is already built-in to construct dictionaries.I think a for-loop would be preferable over a while-loop in this case, it's more straightforward, less clunky, and easier to understand.
You update
curr_length
, but never actually use it to determine yourmax_length
.The docstring should come right after the
class Solution
and before the first function, and no line should be longer than 79 characters. Moreover, you seem to not have pasted the entire docstrings, it just ends mid-sentence.
add a comment |Â
up vote
7
down vote
Nice solution, please find a few thoughts below:
Do you need to wrap it in class for leetcode? It's unnecessary otherwise.
Function names use
snake_case
and notcamelCase
, check PEP8 for more details. Hence it would belength_of_longest_substring
.Do not use
dict
as a variable name, it is already built-in to construct dictionaries.I think a for-loop would be preferable over a while-loop in this case, it's more straightforward, less clunky, and easier to understand.
You update
curr_length
, but never actually use it to determine yourmax_length
.The docstring should come right after the
class Solution
and before the first function, and no line should be longer than 79 characters. Moreover, you seem to not have pasted the entire docstrings, it just ends mid-sentence.
add a comment |Â
up vote
7
down vote
up vote
7
down vote
Nice solution, please find a few thoughts below:
Do you need to wrap it in class for leetcode? It's unnecessary otherwise.
Function names use
snake_case
and notcamelCase
, check PEP8 for more details. Hence it would belength_of_longest_substring
.Do not use
dict
as a variable name, it is already built-in to construct dictionaries.I think a for-loop would be preferable over a while-loop in this case, it's more straightforward, less clunky, and easier to understand.
You update
curr_length
, but never actually use it to determine yourmax_length
.The docstring should come right after the
class Solution
and before the first function, and no line should be longer than 79 characters. Moreover, you seem to not have pasted the entire docstrings, it just ends mid-sentence.
Nice solution, please find a few thoughts below:
Do you need to wrap it in class for leetcode? It's unnecessary otherwise.
Function names use
snake_case
and notcamelCase
, check PEP8 for more details. Hence it would belength_of_longest_substring
.Do not use
dict
as a variable name, it is already built-in to construct dictionaries.I think a for-loop would be preferable over a while-loop in this case, it's more straightforward, less clunky, and easier to understand.
You update
curr_length
, but never actually use it to determine yourmax_length
.The docstring should come right after the
class Solution
and before the first function, and no line should be longer than 79 characters. Moreover, you seem to not have pasted the entire docstrings, it just ends mid-sentence.
edited Aug 12 at 2:21
Daniel
4,1232836
4,1232836
answered Aug 11 at 22:26
Daniel Lenz
32115
32115
add a comment |Â
add a comment |Â
up vote
3
down vote
Minor technical issues
There are a couple of minor technical issues with the solution:
When looping over an iterable, if you need both the index and the value of the current item, it's good to use
enumerate
, so that you cannot accidentally forget to increment the loop counter, and syntax is nicely compact yet readable.curr_length
is modified but never used: should be removed.The code is most readable when there is one statement per line. So I recommend to split multi-assignments like
start, end, curr_length, max_length = 0, 0, 0, 0
. Simple multi-assignments with 2 terms are usually ok though, for exampleleft, right = ...
when implementing a binary searchThe parentheses are unnecessary around the condition in
while end < len(s):
It's unfortunate use names that are types in many languages, such as
dict
andchar
PEP8 recommends to indent with 4 spaces. And it strongly recommends to use the same indent width consistently. (The posted code indents the class body by 4 spaces, but everything else with 2.)
Alternative implementation
Putting the above tips together, and throwing in some doctests:
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
>>> Solution().lengthOfLongestSubstring("bpfbhmipx")
7
>>> Solution().lengthOfLongestSubstring("abcabcbb")
3
>>> Solution().lengthOfLongestSubstring("bbbbb")
1
>>> Solution().lengthOfLongestSubstring("pwwkew")
3
>>> Solution().lengthOfLongestSubstring("tmmzuxt")
5
>>> Solution().lengthOfLongestSubstring("a")
1
>>> Solution().lengthOfLongestSubstring("")
0
>>> Solution().lengthOfLongestSubstring("abc")
3
"""
longest = 0
start = -1
last =
for i, c in enumerate(s):
if c in last:
start = max(start, last[c])
longest = max(longest, i - start)
last[c] = i
return longest
add a comment |Â
up vote
3
down vote
Minor technical issues
There are a couple of minor technical issues with the solution:
When looping over an iterable, if you need both the index and the value of the current item, it's good to use
enumerate
, so that you cannot accidentally forget to increment the loop counter, and syntax is nicely compact yet readable.curr_length
is modified but never used: should be removed.The code is most readable when there is one statement per line. So I recommend to split multi-assignments like
start, end, curr_length, max_length = 0, 0, 0, 0
. Simple multi-assignments with 2 terms are usually ok though, for exampleleft, right = ...
when implementing a binary searchThe parentheses are unnecessary around the condition in
while end < len(s):
It's unfortunate use names that are types in many languages, such as
dict
andchar
PEP8 recommends to indent with 4 spaces. And it strongly recommends to use the same indent width consistently. (The posted code indents the class body by 4 spaces, but everything else with 2.)
Alternative implementation
Putting the above tips together, and throwing in some doctests:
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
>>> Solution().lengthOfLongestSubstring("bpfbhmipx")
7
>>> Solution().lengthOfLongestSubstring("abcabcbb")
3
>>> Solution().lengthOfLongestSubstring("bbbbb")
1
>>> Solution().lengthOfLongestSubstring("pwwkew")
3
>>> Solution().lengthOfLongestSubstring("tmmzuxt")
5
>>> Solution().lengthOfLongestSubstring("a")
1
>>> Solution().lengthOfLongestSubstring("")
0
>>> Solution().lengthOfLongestSubstring("abc")
3
"""
longest = 0
start = -1
last =
for i, c in enumerate(s):
if c in last:
start = max(start, last[c])
longest = max(longest, i - start)
last[c] = i
return longest
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Minor technical issues
There are a couple of minor technical issues with the solution:
When looping over an iterable, if you need both the index and the value of the current item, it's good to use
enumerate
, so that you cannot accidentally forget to increment the loop counter, and syntax is nicely compact yet readable.curr_length
is modified but never used: should be removed.The code is most readable when there is one statement per line. So I recommend to split multi-assignments like
start, end, curr_length, max_length = 0, 0, 0, 0
. Simple multi-assignments with 2 terms are usually ok though, for exampleleft, right = ...
when implementing a binary searchThe parentheses are unnecessary around the condition in
while end < len(s):
It's unfortunate use names that are types in many languages, such as
dict
andchar
PEP8 recommends to indent with 4 spaces. And it strongly recommends to use the same indent width consistently. (The posted code indents the class body by 4 spaces, but everything else with 2.)
Alternative implementation
Putting the above tips together, and throwing in some doctests:
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
>>> Solution().lengthOfLongestSubstring("bpfbhmipx")
7
>>> Solution().lengthOfLongestSubstring("abcabcbb")
3
>>> Solution().lengthOfLongestSubstring("bbbbb")
1
>>> Solution().lengthOfLongestSubstring("pwwkew")
3
>>> Solution().lengthOfLongestSubstring("tmmzuxt")
5
>>> Solution().lengthOfLongestSubstring("a")
1
>>> Solution().lengthOfLongestSubstring("")
0
>>> Solution().lengthOfLongestSubstring("abc")
3
"""
longest = 0
start = -1
last =
for i, c in enumerate(s):
if c in last:
start = max(start, last[c])
longest = max(longest, i - start)
last[c] = i
return longest
Minor technical issues
There are a couple of minor technical issues with the solution:
When looping over an iterable, if you need both the index and the value of the current item, it's good to use
enumerate
, so that you cannot accidentally forget to increment the loop counter, and syntax is nicely compact yet readable.curr_length
is modified but never used: should be removed.The code is most readable when there is one statement per line. So I recommend to split multi-assignments like
start, end, curr_length, max_length = 0, 0, 0, 0
. Simple multi-assignments with 2 terms are usually ok though, for exampleleft, right = ...
when implementing a binary searchThe parentheses are unnecessary around the condition in
while end < len(s):
It's unfortunate use names that are types in many languages, such as
dict
andchar
PEP8 recommends to indent with 4 spaces. And it strongly recommends to use the same indent width consistently. (The posted code indents the class body by 4 spaces, but everything else with 2.)
Alternative implementation
Putting the above tips together, and throwing in some doctests:
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
>>> Solution().lengthOfLongestSubstring("bpfbhmipx")
7
>>> Solution().lengthOfLongestSubstring("abcabcbb")
3
>>> Solution().lengthOfLongestSubstring("bbbbb")
1
>>> Solution().lengthOfLongestSubstring("pwwkew")
3
>>> Solution().lengthOfLongestSubstring("tmmzuxt")
5
>>> Solution().lengthOfLongestSubstring("a")
1
>>> Solution().lengthOfLongestSubstring("")
0
>>> Solution().lengthOfLongestSubstring("abc")
3
"""
longest = 0
start = -1
last =
for i, c in enumerate(s):
if c in last:
start = max(start, last[c])
longest = max(longest, i - start)
last[c] = i
return longest
answered Aug 12 at 8:50
janos
95.6k12120343
95.6k12120343
add a comment |Â
add a comment |Â
up vote
2
down vote
just a note, for string iteration, a for loop might be more suitable
from
while ( end < len(s)):
char = s[end]
...
end += 1
to
for end, char in enumerate(s)
...
@Mathias Ettinger edited
â Abdur-Rahmaan Janhangeer
Aug 12 at 8:51
add a comment |Â
up vote
2
down vote
just a note, for string iteration, a for loop might be more suitable
from
while ( end < len(s)):
char = s[end]
...
end += 1
to
for end, char in enumerate(s)
...
@Mathias Ettinger edited
â Abdur-Rahmaan Janhangeer
Aug 12 at 8:51
add a comment |Â
up vote
2
down vote
up vote
2
down vote
just a note, for string iteration, a for loop might be more suitable
from
while ( end < len(s)):
char = s[end]
...
end += 1
to
for end, char in enumerate(s)
...
just a note, for string iteration, a for loop might be more suitable
from
while ( end < len(s)):
char = s[end]
...
end += 1
to
for end, char in enumerate(s)
...
edited Aug 12 at 8:50
answered Aug 12 at 6:35
Abdur-Rahmaan Janhangeer
21811
21811
@Mathias Ettinger edited
â Abdur-Rahmaan Janhangeer
Aug 12 at 8:51
add a comment |Â
@Mathias Ettinger edited
â Abdur-Rahmaan Janhangeer
Aug 12 at 8:51
@Mathias Ettinger edited
â Abdur-Rahmaan Janhangeer
Aug 12 at 8:51
@Mathias Ettinger edited
â Abdur-Rahmaan Janhangeer
Aug 12 at 8:51
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f201483%2flongest-substring-without-repeating-characters-in-python%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password