-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlc-3.py
More file actions
16 lines (13 loc) · 744 Bytes
/
Copy pathlc-3.py
File metadata and controls
16 lines (13 loc) · 744 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Given a string s, find the length of the longest substring without repeating characters.
class Solution:
def lengthOfLongestSubstring(self, s):
char_set = set() # Set to store unique characters
left = 0 # Left pointer of the sliding window
max_length = 0 # Maximum length of substring without repeating characters
for right in range(len(s)):
while s[right] in char_set: # Shrink the window until the repeating character is removed
char_set.remove(s[left])
left += 1
char_set.add(s[right]) # Add the current character to the set
max_length = max(max_length, right - left + 1) # Update the maximum length
return max_length