Tensorum
YouTubeশেখা শুরু করুন
LeetCode 290 · Hashing / Frequency

Word Pattern

LeetCode 290 · সহজ · সমাধান, intuition ও ধাপে ধাপে ওয়াকথ্রু

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

সহজHash TableString
LeetCode-এ খুলুন ↗

আমি 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.