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 * 104
s
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 strategy is to look at every single possible continuous piece of text, one by one. For each piece, we'll check if it has any repeated characters. We'll keep track of the longest one we've found so far that has no repeats.
Here's how the algorithm would work step-by-step:
def length_of_longest_substring_brute_force(input_string):
max_length_so_far = 0
number_of_characters = len(input_string)
# This outer loop defines the starting point for every possible substring.
for start_index in range(number_of_characters):
# This inner loop defines the end point, thus generating all substrings from start_index.
for end_index in range(start_index, number_of_characters):
substring = input_string[start_index:end_index + 1]
seen_characters = set()
has_duplicate = False
for character in substring:
# We use a set to efficiently check for duplicates within the current substring.
if character in seen_characters:
has_duplicate = True
break
seen_characters.add(character)
if not has_duplicate:
max_length_so_far = max(max_length_so_far, len(substring))
return max_length_so_far
Imagine you have an expanding window that slides over the text. The goal is to stretch this window as wide as possible to cover a piece of text with no duplicate characters. We keep track of the largest window size we've seen so far.
Here's how the algorithm would work step-by-step:
def length_of_longest_substring(input_string):
characters_in_window = set()
left_pointer = 0
max_length = 0
for right_pointer in range(len(input_string)):
current_character = input_string[right_pointer]
# If a character repeats, we must shrink the window from the left until the duplicate is removed.
while current_character in characters_in_window:
character_to_remove = input_string[left_pointer]
characters_in_window.remove(character_to_remove)
left_pointer += 1
# Add the new character to the window, effectively expanding it.
characters_in_window.add(current_character)
# After each valid expansion, check if we have a new longest substring.
current_window_size = right_pointer - left_pointer + 1
max_length = max(max_length, current_window_size)
return max_length
Case | How to Handle |
---|---|
Input string is empty | The algorithm should return 0 as there are no substrings. |
Input string contains only one character | The longest substring is the string itself, so the function should return 1. |
Input string contains all unique characters | The entire string is the longest substring without repeating characters, so its length should be returned. |
Input string contains all identical characters | The longest substring without repeating characters will always have a length of 1. |
Input string contains special characters, spaces, or numbers | The solution should handle any valid character from the character set correctly by tracking its last seen position. |
The longest substring appears at the very beginning of the string | The sliding window correctly finds the maximum length early and maintains it throughout the remaining scan. |
The longest substring appears at the very end of the string | The algorithm's final check after the loop completes ensures the last potential substring is considered for the maximum length. |
Maximum-sized input string (e.g., 5 * 10^4 characters) | An efficient O(N) sliding window approach ensures the solution runs within time limits without performance degradation. |