-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongest_Substring_Without_Repeating_Characters.java
40 lines (31 loc) · 1.26 KB
/
Longest_Substring_Without_Repeating_Characters.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```bash
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
# Create a set to keep track of unique characters
unique_chars = set()
n = len(s) # Length of the string
left = 0 # Left pointer of the sliding window
max_length = 0 # Maximum length of substring without repeating characters
# Iterate through the string with the right pointer
for right in range(n):
# Check if the current character is already in the set
while s[right] in unique_chars:
# If it is, remove the leftmost character from the set
unique_chars.remove(s[left])
left += 1
# Add the current character to the set
unique_chars.add(s[right])
# Update the maximum length if necessary
max_length = max(max_length, right - left + 1)
return max_length
```