How can you find the longest substring without repeating characters using Python?
Posted by JackBrn
Last Updated: August 27, 2024
Finding the longest substring without repeating characters is a common problem in programming that can be solved efficiently using Python. The goal is to identify and return the length of the longest substring that contains all distinct characters from a given string. Here’s a detailed approach to solving this problem using a sliding window technique along with a hash map.
Approach
1. Sliding Window Technique: This method involves using two pointers to create a window that can expand and contract, helping to track the current substring without repeating characters. 2. Hash Map for Tracking Characters: A hash map (dictionary in Python) is used to store the last seen index of each character, allowing us to quickly determine if a character has been seen before and where it can be safely included in the substring.
Steps
1. Initialize two pointers left and right, which represent the current window of characters. 2. Use a dictionary to keep track of the last index of each character currently in the window. 3. Initialize a variable to store the maximum length found. 4. Iterate through the string using the right pointer. For each character: - If the character is already in the current substring (left pointer's range), move the left pointer to exclude the previous occurrence of the character from the substring. - Update the last seen index of the character. - Calculate the length of the current substring and update the maximum length if necessary.
Implementation
Here is a Python function that implements the above logic:
def longest_substring_without_repeating(s: str) -> int:
    char_index_map = {}
    max_length = 0
    left = 0  # Left pointer

    for right in range(len(s)):
        current_char = s[right]
        # If character is already in the substring, move the left pointer
        if current_char in char_index_map and char_index_map[current_char] >= left:
            left = char_index_map[current_char] + 1

        # Update the last seen index of the character
        char_index_map[current_char] = right

        # Calculate the length of the current substring
        max_length = max(max_length, right - left + 1)

    return max_length
Example Usage
input_string = "abcabcbb"
result = longest_substring_without_repeating(input_string)
print(f"The length of the longest substring without repeating characters is: {result}")
Conclusion
This algorithm runs in O(n) time complexity, where n is the length of the input string. It efficiently scans through the string while maintaining a dynamic length of substrings without duplicates, making it suitable for large strings. The use of the sliding window technique ensures that the solution is optimal without needing nested loops, which would increase the time complexity. This function can be easily modified to return the substring itself or its starting index, depending on specific requirements.
Related Content