Tensorum
YouTubeशिकायला सुरुवात करा
LeetCode 290 · Hashing / Frequency

Word Pattern

LeetCode 290 · सोपा · उपाय, intuition आणि टप्प्याटप्प्याने वॉकथ्रू

Last-seen index per letter and per word must agree.

सोपाHash TableString

मी Word Pattern (LeetCode 290) असा सोडवला — Hashing / Frequency पॅटर्नमधील एक सोपा प्रॉब्लेम. खाली: intuition, टप्प्याटप्प्याने approach, माझा accepted Python कोड, animated test cases आणि line-by-line debugger.

खालील विवरण इंग्रजीत आहे.

Intuition

Same idea as isomorphic strings, but between pattern letters and words: each letter and its word must have been last seen at the same index.

Approach

  1. 1

    Split s into words; lengths must match.

  2. 2

    Compare stored indices for pattern[i] and words[i].

  3. 3

    Record the current index for both.

मी कसा सोडवला

Last-seen indicesPythonO(n) time · O(n) space
class Solution:
    def wordPattern(self, pattern: str, s: str) -> bool:
        pattern_mpp = {}
        s_mpp = {}
        words = s.split(" ")
        if len(pattern)!=len(words):
            return False
        for i in range(len(pattern)):
            char = pattern[i]
            word = words[i]
            if pattern_mpp.get(char,-1)!=s_mpp.get(word,-1):
                return False
            pattern_mpp[char]=i
            s_mpp[word]=i
        return True
स्टेप-थ्रू लोड होत आहे…

Word Pattern — वारंवार विचारले जाणारे प्रश्न

Word Pattern मागील intuition काय आहे?

Same idea as isomorphic strings, but between pattern letters and words: each letter and its word must have been last seen at the same index.

Word Pattern (LeetCode 290) कसा सोडवावा?

1. Split s into words; lengths must match. 2. Compare stored indices for pattern[i] and words[i]. 3. Record the current index for both.

Word Pattern ची time आणि space complexity किती आहे?

Time: O(n). Space: O(n). (Accepted approach: Last-seen indices.)

Word Pattern मध्ये सामान्य चुका कोणत्या?

Length mismatch is an immediate False.