"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 &lt; 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.
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 not camelCase, check PEP8 for more details. Hence it would be length_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 your max_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.