3. Longest Substring Without Repeating Characters - Medium
Given a string s, find the length of the longest substring without repeating characters.
Example 1:
Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
Example 3:
Input: s = ""
Output: 0
Solution:
Two Pointers - Sliding Window, Hash Set for detecting duplicates, Space O(N), Time O(N)
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
longest = 0
visited = set()
j = 0
for i in range(len(s)):
while j < len(s) and s[j] not in visited:
visited.add(s[j])
j += 1
longest = max(longest, j - i)
# sliding the window,
# by removing the i ptr element in visited
visited.remove(s[i])
# prunning
if j >= len(s):
break
return longest