Tensorum
YouTubeనేర్చుకోవడం ప్రారంభించండి
LeetCode 3 · Variable Window – Longest

Longest Substring Without Repeating Characters

LeetCode 3 · మధ్యస్థం · పరిష్కారం, intuition & దశలవారీ వాక్‌త్రూ

On a repeat, l = max(l, last[ch] + 1).

మధ్యస్థంHash TableStringSliding Window
LeetCode లో తెరవండి ↗

నేను Longest Substring Without Repeating Characters (LeetCode 3) ను ఇలా పరిష్కరించాను — Variable Window – Longest ప్యాటర్న్‌లోని ఒక మధ్యస్థం ప్రాబ్లమ్. కింద: intuition, దశలవారీ approach, నా accepted Python కోడ్, animated test cases, line-by-line debugger.

కింది వివరణ ఇంగ్లీష్‌లో ఉంది.

Intuition

Grow a window to the right. When the new character already appears inside the window, jump the left edge just past its previous occurrence. The window always holds distinct characters, so its size is a candidate answer.

Approach

  1. 1

    seen maps character → last index.

  2. 2

    If s[r] was seen, move l past it (never backwards).

  3. 3

    Update the max length and seen[s[r]] = r.

నేను ఎలా పరిష్కరించాను

Sliding window + last indexPythonO(n) time
class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        seen = {}
        l=0
        r=0
        max_length=0
        while r<len(s):
            if s[r] in seen:
                l= max(seen.get(s[r],0)+1,l)
            max_length = max(max_length,r-l+1)
            seen[s[r]]=r
            r+=1
        return max_length
స్టెప్-త్రూ లోడ్ అవుతోంది…

Longest Substring Without Repeating Characters — తరచూ అడిగే ప్రశ్నలు

Longest Substring Without Repeating Characters వెనుక ఉన్న intuition ఏమిటి?

Grow a window to the right. When the new character already appears inside the window, jump the left edge just past its previous occurrence. The window always holds distinct characters, so its size is a candidate answer.

Longest Substring Without Repeating Characters (LeetCode 3) ను ఎలా పరిష్కరించాలి?

1. seen maps character → last index. 2. If s[r] was seen, move l past it (never backwards). 3. Update the max length and seen[s[r]] = r.

Longest Substring Without Repeating Characters యొక్క time, space complexity ఎంత?

Time: O(n). Space: O(Σ). (Accepted approach: Sliding window + last index.)

Longest Substring Without Repeating Characters లో సాధారణ పొరపాట్లు ఏమిటి?

Use max() — an old occurrence before l must not drag l backwards ('abba').