leetcode-study

3. Longest Substring Without Repeating Characters

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        """
        Find the length of the longest substring without repeating characters.
        
        Parameters:
            s (str): The input string consisting of letters, digits, symbols, and spaces.
        
        Returns:
            int: The length of the longest substring that contains no duplicate characters.
        """
        char_index = {}  # Dictionary to store the most recent index of each character.
        longest = 0      # Variable to keep track of the maximum length found.
        start = 0        # Left pointer for the sliding window.

        # Iterate over each character in the string with its index.
        for i, char in enumerate(s):
            # If the character is found in the dictionary and is within the current window,
            # move the start pointer to one position right of its last occurrence.
            if char in char_index and char_index[char] >= start:
                start = char_index[char] + 1
            
            # Update the current character's index in the dictionary.
            char_index[char] = i
            # Calculate the length of the current substring and update longest if needed.
            longest = max(longest, i - start + 1)
        
        return longest

Summary of Techniques and Approaches: