Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 567 · Fixed Window

Permutation in String

LeetCode 567 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

Fixed-size window with matching counts.

நடுத்தரம்Hash TableTwo PointersStringSliding Window
LeetCode இல் திற ↗

நான் Permutation in String (LeetCode 567) ஐ இப்படித் தீர்த்தேன் — Fixed Window பேட்டர்னின் ஒரு நடுத்தரம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.

Intuition

A permutation of s1 is just a window of the same length in s2 with identical letter counts. Slide a window of size len(s1) over s2 and compare counts.

Approach

  1. 1

    Count s1.

  2. 2

    Add s2[r]; drop s2[l] when the window exceeds len(s1).

  3. 3

    Return True as soon as counts are equal.

நான் எப்படித் தீர்த்தேன்

Sliding window countsPythonO(n) time
class Solution:
    def checkInclusion(self, s1: str, s2: str) -> bool:
        mpp = defaultdict(int)
        for char in s1:
            mpp[char]+=1
        l=0
        r=0
        cur_freq_mpp=defaultdict(int)
        while r<len(s2):
            cur = s2[r]
            cur_freq_mpp[cur]+=1
            if r-l+1 > len(s1):
                #shrink
                cur_freq_mpp[s2[l]]-=1
                if cur_freq_mpp[s2[l]]==0:
                    del cur_freq_mpp[s2[l]]
                l+=1
            if mpp==cur_freq_mpp:
                return True
            r+=1
        return False
ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Permutation in String — அடிக்கடி கேட்கப்படும் கேள்விகள்

Permutation in String பின்னாலுள்ள intuition என்ன?

A permutation of s1 is just a window of the same length in s2 with identical letter counts. Slide a window of size len(s1) over s2 and compare counts.

Permutation in String (LeetCode 567) ஐ எப்படித் தீர்ப்பது?

1. Count s1. 2. Add s2[r]; drop s2[l] when the window exceeds len(s1). 3. Return True as soon as counts are equal.

Permutation in String இன் time மற்றும் space complexity என்ன?

Time: O(n). Space: O(Σ). (Accepted approach: Sliding window counts.)

Permutation in String இல் பொதுவான தவறுகள் என்ன?

Delete zero counts so dict comparison is accurate.