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.