Longest Substring Without Repeating

interview_workbook/leetcode/sliding_window /app/src/interview_workbook/leetcode/sliding_window/longest_substring_without_repeating.py
View Source

Algorithm Notes

Summary: Longest Substring Without Repeating — notes not yet curated.
Time: Estimate via loops/recurrences; common classes: O(1), O(log n), O(n), O(n log n), O(n^2)
Space: Count auxiliary structures and recursion depth.
Tip: See the Big-O Guide for how to derive bounds and compare trade-offs.

Big-O Guide

Source

"""
Longest Substring Without Repeating

Problem: Longest Substring Without Repeating Characters
LeetCode link: https://leetcode.com/problems/longest-substring-without-repeating-characters/
Description: Given a string, find the length of the longest substring without repeating characters.
"""

from src.interview_workbook.leetcode._registry import register_problem
from src.interview_workbook.leetcode._types import Category, Difficulty


class Solution:
    def solve(self, *args):
        """Return length of longest substring without repeating characters."""
        (s,) = args
        seen = {}
        l = 0
        res = 0
        for r, ch in enumerate(s):
            if ch in seen and seen[ch] >= l:
                l = seen[ch] + 1
            seen[ch] = r
            res = max(res, r - l + 1)
        return res


def demo():
    """TODO: Implement demo function."""
    pass


register_problem(
    id=3,
    slug="longest_substring_without_repeating",
    title="Longest Substring Without Repeating Characters",
    category=Category.SLIDING_WINDOW,
    difficulty=Difficulty.MEDIUM,
    tags=["string", "sliding_window", "hashmap"],
    url="https://leetcode.com/problems/longest-substring-without-repeating-characters/",
    notes="",
)