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.