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 method for this problem is simple: try every possible substring. We check each one to see if it has any repeated characters and remember the longest one we find that doesn't.
Here's how the algorithm would work step-by-step:
def longest_substring_without_repeating_characters_brute_force(input_string):
longest_length = 0
string_length = len(input_string)
for start_index in range(string_length):
for end_index in range(start_index, string_length):
substring = input_string[start_index:end_index + 1]
# Check if the current substring has repeating characters
if len(set(substring)) == len(substring):
# Update the longest length if needed
longest_length = max(longest_length, len(substring))
return longest_length
The core idea is to maintain a window that represents a possible substring. We slide this window across the string, expanding it when we find unique characters and shrinking it when we encounter a repeated character. This way, we only consider valid substrings.
Here's how the algorithm would work step-by-step:
def length_of_longest_substring(input_string):
start_index = 0
end_index = 0
maximum_length = 0
character_index_map = {}
while end_index < len(input_string):
current_character = input_string[end_index]
# If char exists and within window
if current_character in character_index_map and character_index_map[current_character] >= start_index:
start_index = character_index_map[current_character] + 1
character_index_map[current_character] = end_index
# Update max length
maximum_length = max(maximum_length, end_index - start_index + 1)
end_index += 1
return maximum_length
Case | How to Handle |
---|---|
Empty string | Return 0 if the input string is empty as there are no characters to form a substring. |
String with only one character | Return 1 if the string has only one character because that single character forms a substring of length 1. |
String with all identical characters (e.g., 'aaaa') | The algorithm should correctly identify the longest substring without repeating characters, which will be of length 1 in this case. |
String with consecutive repeating characters (e.g., 'aab') | Sliding window should move past the initial 'aa' and find 'ab' with length 2. |
String with non-ASCII characters (e.g., Unicode) | Ensure the character tracking data structure (e.g., hash map) can handle a wider range of characters and their indices correctly. |
Long string approaching memory limits | Sliding window approach uses constant space, so it should handle long strings until memory limits are actually hit. |
String with maximum possible length | Check for integer overflow in length calculations and that the algorithm scales linearly O(n). |
String with alternating repeating characters (e.g., 'abababa') | The sliding window algorithm should efficiently find the longest non-repeating substring within the given pattern. |