Word Pattern
LeetCode 290 · সহজ · সমাধান, intuition ও ধাপে ধাপে ওয়াকথ্রু
Last-seen index per letter and per word must agree.
আমি 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
Split s into words; lengths must match.
- 2
Compare stored indices for pattern[i] and words[i].
- 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 TrueWord 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.