-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongestSubstringWithoutRepeatingChars.py
More file actions
24 lines (16 loc) · 1.06 KB
/
longestSubstringWithoutRepeatingChars.py
File metadata and controls
24 lines (16 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def longestSubstringWithoutRepeatingChars(s):
charMap = {} # Dictionary to store the last seen index of each character
left = 0 # Left pointer of the current substring window
maxLength = 0 # Stores the length of the longest valid substring found
for right in range(len(s)): # Iterate through each character in the string using right pointer
# If current character is already seen and is inside the current window
if s[right] in charMap and charMap[s[right]] >= left:
left = charMap[s[right]] + 1 # Move left pointer to one position after last seen index
charMap[s[right]] = right # Update the last seen index of the current character
# Calculate the length of the current window and update maxLength if it's larger
maxLength = max(maxLength, right - left + 1)
return maxLength # Return the length of the longest substring without repeating characters
# Example Usage
s = "abcabcbb"
result = longestSubstringWithoutRepeatingChars(s)
print(result)