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