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.