Solving the Longest Substring Without Repeating Characters Problem in Python | O(n) Time Complexity

Solving the Longest Substring Without Repeating Characters Problem in Python | O(n) Time Complexity

Understanding the Problem

The core challenge of this problem is to find the length of the longest substring without repeating characters in a given string. This problem is significant in various applications such as text processing, data compression, and more. A common pitfall is to overlook the need for an efficient solution that can handle large input sizes.

Approach

To solve this problem, we need to consider different approaches:

Naive Solution

The naive solution involves checking all possible substrings and determining if they contain repeating characters. This approach is not optimal due to its high time complexity of O(n^3).

Optimized Solution

An optimized solution uses a sliding window technique with a hash map to keep track of characters and their positions. This approach ensures that we only traverse the string once, achieving a time complexity of O(n).

Algorithm

Here is a step-by-step breakdown of the optimized algorithm:

  1. Initialize a hash map to store the last seen position of each character.
  2. Use two pointers to represent the current window of characters being considered.
  3. Expand the window by moving the right pointer and update the hash map.
  4. If a repeating character is found, move the left pointer to the right of the last seen position of that character.
  5. Keep track of the maximum length of substrings found during the process.

Code Implementation

def length_of_longest_substring(s: str) -> int:
    # Hash map to store the last seen position of each character
    char_map = {}
    # Initialize the left pointer and the maximum length
    left = 0
    max_length = 0

    # Iterate over the string with the right pointer
    for right in range(len(s)):
        # If the character is already in the hash map and its last seen position is within the current window
        if s[right] in char_map and char_map[s[right]] >= left:
            # Move the left pointer to the right of the last seen position
            left = char_map[s[right]] + 1
        # Update the last seen position of the character
        char_map[s[right]] = right
        # Update the maximum length
        max_length = max(max_length, right - left + 1)

    return max_length
    

Complexity Analysis

The time complexity of the optimized solution is O(n) because we traverse the string once. The space complexity is O(min(n, m)), where n is the length of the string and m is the size of the character set.

Edge Cases

Consider the following edge cases:

Testing

To test the solution comprehensively, consider a variety of test cases:

Using a testing framework like unittest or pytest can help automate and manage these tests effectively.

Thinking and Problem-Solving Tips

When approaching such problems, consider the following tips:

Conclusion

In this blog post, we discussed how to solve the Longest Substring Without Repeating Characters problem using an optimized approach with a sliding window technique. Understanding and solving such problems is crucial for improving algorithmic thinking and coding skills.

Additional Resources

For further reading and practice, consider the following resources: