Tensorum
YouTubeशिकायला सुरुवात करा
LeetCode 567 · Fixed Window

Permutation in String

LeetCode 567 · मध्यम · उपाय, intuition आणि टप्प्याटप्प्याने वॉकथ्रू

Fixed-size window with matching counts.

मध्यमHash TableTwo PointersStringSliding Window

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