longest_substring_without_repeating_characters
1from collections import Counter 2 3 4# @leet start 5class Solution: 6 def lengthOfLongestSubstring(self, s: str) -> int: 7 """ 8 To calculate the length of the longest substring, we want to 9 create a sliding window, where the current window does not have 10 any repeating characters. 11 12 To handle this, we have 3 variables, a left and right pointer, 13 and a Counter, which counts the chars that are included. 14 15 If two of the same char are encountered, then we keep advancing the left 16 pointer until we either reach the right pointer or we've removed 17 the second time the char was included. 18 19 For each iteration, we check to see if it's the max length we've seen so far 20 and continue on. 21 22 """ 23 chars = Counter() 24 25 left = right = 0 26 27 res = 0 28 while right < len(s): 29 r = s[right] 30 chars[r] += 1 31 32 while chars[r] > 1: 33 l = s[left] 34 chars[l] -= 1 35 left += 1 36 37 res = max(res, right - left + 1) 38 39 right += 1 40 return res 41 42 43# @leet end 44 45 46def test(): 47 assert 2 + 2 == 4
class
Solution:
6class Solution: 7 def lengthOfLongestSubstring(self, s: str) -> int: 8 """ 9 To calculate the length of the longest substring, we want to 10 create a sliding window, where the current window does not have 11 any repeating characters. 12 13 To handle this, we have 3 variables, a left and right pointer, 14 and a Counter, which counts the chars that are included. 15 16 If two of the same char are encountered, then we keep advancing the left 17 pointer until we either reach the right pointer or we've removed 18 the second time the char was included. 19 20 For each iteration, we check to see if it's the max length we've seen so far 21 and continue on. 22 23 """ 24 chars = Counter() 25 26 left = right = 0 27 28 res = 0 29 while right < len(s): 30 r = s[right] 31 chars[r] += 1 32 33 while chars[r] > 1: 34 l = s[left] 35 chars[l] -= 1 36 left += 1 37 38 res = max(res, right - left + 1) 39 40 right += 1 41 return res
def
lengthOfLongestSubstring(self, s: str) -> int:
7 def lengthOfLongestSubstring(self, s: str) -> int: 8 """ 9 To calculate the length of the longest substring, we want to 10 create a sliding window, where the current window does not have 11 any repeating characters. 12 13 To handle this, we have 3 variables, a left and right pointer, 14 and a Counter, which counts the chars that are included. 15 16 If two of the same char are encountered, then we keep advancing the left 17 pointer until we either reach the right pointer or we've removed 18 the second time the char was included. 19 20 For each iteration, we check to see if it's the max length we've seen so far 21 and continue on. 22 23 """ 24 chars = Counter() 25 26 left = right = 0 27 28 res = 0 29 while right < len(s): 30 r = s[right] 31 chars[r] += 1 32 33 while chars[r] > 1: 34 l = s[left] 35 chars[l] -= 1 36 left += 1 37 38 res = max(res, right - left + 1) 39 40 right += 1 41 return res
To calculate the length of the longest substring, we want to create a sliding window, where the current window does not have any repeating characters.
To handle this, we have 3 variables, a left and right pointer, and a Counter, which counts the chars that are included.
If two of the same char are encountered, then we keep advancing the left pointer until we either reach the right pointer or we've removed the second time the char was included.
For each iteration, we check to see if it's the max length we've seen so far and continue on.
def
test():