Given a string s, find the length of the longest substring without duplicate characters.
Example 1:
Input: s = "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: s = "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1.
Example 3:
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.
Constraints:
0 <= s.length <= 5 * 104s consists of English letters, digits, symbols and spaces.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding the longest sequence of unique characters involves examining every single possible continuous slice of the given text. We methodically check every possible starting point and ending point to see if any repeated characters sneak in, and we keep track of the biggest valid slice we find.
Here's how the algorithm would work step-by-step:
def length_of_longest_substring(input_string):
max_length = 0
# Test every possible starting position to examine all continuous slices
for start_index in range(len(input_string)):
for end_index in range(start_index, len(input_string)):
current_slice = input_string[start_index:end_index + 1]
# Check for duplicates because valid slices must contain only unique characters
if len(current_slice) == len(set(current_slice)):
current_length = len(current_slice)
# Keep track of the biggest valid slice found during exhaustive search
if current_length > max_length:
max_length = current_length
return max_lengthInstead of checking every single possible grouping of letters, the most efficient approach uses a growing window that expands and shrinks dynamically. By keeping track of which letters we have already seen, we can quickly skip past duplicates without starting our search all over again.
Here's how the algorithm would work step-by-step:
def length_of_longest_substring(input_string):
last_seen_indices = {}
start_index_of_window = 0
max_substring_length = 0
# Iterate through the string, treating the current character as the front edge of our frame.
for current_index, character in enumerate(input_string):
if character in last_seen_indices and last_seen_indices[character] >= start_index_of_window:
# Shrink the window from the back edge to drop the duplicate character and everything before it.
start_index_of_window = last_seen_indices[character] + 1
last_seen_indices[character] = current_index
# Measure the current window size and keep track of the largest size achieved so far.
current_window_size = current_index - start_index_of_window + 1
if current_window_size > max_substring_length:
max_substring_length = current_window_size
return max_substring_length| Case | How to Handle |
|---|---|
| Empty string input where length is zero | The sliding window loop safely terminates immediately and returns zero. |
| String with a single character | The window size remains one and the function correctly returns one. |
| String with all identical characters such as aaaaa | The left pointer jumps past the previous duplicate ensuring the window length never exceeds one. |
| String with no repeating characters such as abcde | The right pointer expands to the end of the string and returns the total length. |
| Characters repeating far apart in a long string | The hash map stores the latest index of each character allowing the left pointer to jump directly past the old occurrence. |
| String with special characters spaces or unicode | Using an integer frequency array or hash map based on character codes naturally accommodates any valid character set. |
| Maximum length constraints scaling up to large inputs | The optimized sliding window approach operates in linear time complexity visiting each character at most twice. |