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

मी 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').