Skip to main content
Tweeted twitter.com/StackCodeReview/status/1028657441590968326
added 1 character in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Longest "Longest Substring Without Repeating CharactersCharacters" in pythonPython

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.

Longest Substring Without Repeating Characters in 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

"Longest Substring Without Repeating Characters" in 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.

Source Link
NinjaG
  • 2.6k
  • 2
  • 30
  • 61

Longest Substring Without Repeating Characters in 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