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:
This problem asks for the longest sequence of characters within a given text where no character repeats. The brute force approach is like meticulously examining every single possible sequence of characters to see if it's valid and then finding the longest one.
Here's how the algorithm would work step-by-step:
def length_of_longest_substring(input_string):
maximum_length_found = 0
# Iterate through every possible starting point for a substring
for starting_index in range(len(input_string)):
current_substring_characters = set()
current_length = 0
# For each starting point, try extending the substring one character at a time
for ending_index in range(starting_index, len(input_string)):
current_character = input_string[ending_index]
# If the current character is already in our set, this substring is no longer valid
if current_character in current_substring_characters:
break
# Add the unique character to our set and increment the current substring length
current_substring_characters.add(current_character)
current_length += 1
# Update the overall maximum length if the current valid substring is longer
if current_length > maximum_length_found:
maximum_length_found = current_length
return maximum_length_foundThe core idea is to keep track of the characters we've seen within the current sequence. As we explore longer sequences, if we encounter a character already in our current sequence, we need to adjust the start of our sequence to exclude the previous occurrence of that character and everything before it. This way, we efficiently expand and shrink our view of valid sequences.
Here's how the algorithm would work step-by-step:
def length_of_longest_substring(input_string):
longest_substring_length = 0
window_start_index = 0
characters_in_window = {}
for window_end_index, current_character in enumerate(input_string):
if current_character in characters_in_window and characters_in_window[current_character] >= window_start_index:
# When a repeating character is found, we need to move the window's start.
# This ensures the window only contains unique characters.
window_start_index = characters_in_window[current_character] + 1
# Update the last seen index of the current character.
characters_in_window[current_character] = window_end_index
# The current window size represents a valid substring without repeating characters.
current_window_size = window_end_index - window_start_index + 1
# We maintain the maximum length found so far.
longest_substring_length = max(longest_substring_length, current_window_size)
# After iterating through the entire string, we have the overall longest length.
return longest_substring_length| Case | How to Handle |
|---|---|
| Input string is empty | An empty string has no substrings, so the length of the longest substring without repeating characters is 0. |
| Input string has only one character | A single character string is itself the longest substring without repeating characters, so the length is 1. |
| Input string contains all unique characters | The entire string will be the longest substring, and its length will be returned. |
| Input string contains all identical characters | The longest substring without repeating characters will have a length of 1, considering only a single instance of the repeated character. |
| Input string is very long | A sliding window approach with a hash map (or set) provides an efficient O(n) time complexity solution, scaling well with large inputs. |
| Input string contains a mix of repeating and non-repeating characters | The sliding window will dynamically expand and shrink, correctly identifying the longest valid substring. |
| Input string contains special characters, numbers, or spaces | The chosen data structure (e.g., hash map or set) should handle any valid character type present in the string. |
| Multiple longest substrings of the same length exist | The algorithm will find and return the length of one of these longest substrings, not necessarily all of them. |