Tensorum
YouTubeనేర్చుకోవడం ప్రారంభించండి
LeetCode 76 · Sliding Window

Minimum Window Substring

LeetCode 76 · కఠినం · పరిష్కారం, intuition & దశలవారీ వాక్‌త్రూ

Expand to valid, shrink to minimal; 'formed' counts satisfied characters.

కఠినంHash TableStringSliding Window
LeetCode లో తెరవండి ↗

నేను Minimum Window Substring (LeetCode 76) ను ఇలా పరిష్కరించాను — Sliding Window ప్యాటర్న్‌లోని ఒక కఠినం ప్రాబ్లమ్. కింద: intuition, దశలవారీ approach, నా accepted Python కోడ్, animated test cases, line-by-line debugger.

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

Intuition

Expand the right edge until the window contains every required character in the right quantity; then shrink from the left as far as possible while it stays valid, recording the smallest window. Repeat for every right edge.

Approach

  1. 1

    Count requirements from t; required = number of distinct characters.

  2. 2

    Add s[r] to the window; when its count reaches the need, formed++.

  3. 3

    While formed == required: record the window, remove s[l], update formed, l++.

  4. 4

    Return the best window.

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

Sliding window with countsPythonO(n) time
class Solution:
    def minWindow(self, s: str, t: str) -> str:
        req_freq_map = Counter(t)
        required = len(req_freq_map)
        window_map_freq = defaultdict(int)
        l=0
        r=0
        s_index=None
        r_index=None
        formed=0
        min_length = float("inf")

        while r<len(s):
            char=s[r]
            window_map_freq[char]+=1
            if char in req_freq_map and window_map_freq[char]==req_freq_map[char]:
                formed+=1
            #shrink
            while l<=r and formed==required:
                left_most_char = s[l]
                window_map_freq[left_most_char]-=1
                if r-l+1<min_length:
                    min_length = r-l+1
                    s_index=l
                    r_index=r
                if left_most_char in req_freq_map and window_map_freq[left_most_char]<req_freq_map[left_most_char]:
                    formed-=1
                l+=1
            r+=1
        return s[s_index:r_index+1] if  s_index!=None and r_index!=None else ""
స్టెప్-త్రూ లోడ్ అవుతోంది…

Minimum Window Substring — తరచూ అడిగే ప్రశ్నలు

Minimum Window Substring వెనుక ఉన్న intuition ఏమిటి?

Expand the right edge until the window contains every required character in the right quantity; then shrink from the left as far as possible while it stays valid, recording the smallest window. Repeat for every right edge.

Minimum Window Substring (LeetCode 76) ను ఎలా పరిష్కరించాలి?

1. Count requirements from t; required = number of distinct characters. 2. Add s[r] to the window; when its count reaches the need, formed++. 3. While formed == required: record the window, remove s[l], update formed, l++. 4. Return the best window.

Minimum Window Substring యొక్క time, space complexity ఎంత?

Time: O(|s| + |t|). Space: O(Σ). (Accepted approach: Sliding window with counts.)

Minimum Window Substring లో సాధారణ పొరపాట్లు ఏమిటి?

formed should change only when a count crosses the requirement exactly. Return '' when no window is ever valid.