Ransom Note
LeetCode 383 · எளிது · தீர்வு, intuition & படிப்படியான வாக்த்ரூ
Counter(magazine) ≥ Counter(note) letter by letter.
நான் Ransom Note (LeetCode 383) ஐ இப்படித் தீர்த்தேன் — Other பேட்டர்னின் ஒரு எளிது பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.
கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.
Intuition
The note can be built if the magazine has at least as many of each letter as the note needs. Count both and compare.
Approach
- 1
Count letters in both strings.
- 2
Any letter needed more often than available → False.
நான் எப்படித் தீர்த்தேன்
Frequency countsPythonO(m+n) time
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
freq_ransom = Counter(ransomNote)
freq_magazine=Counter(magazine)
for char, freq_ransom_count in freq_ransom.items():
freq_mag_count = freq_magazine[char]
if freq_mag_count<freq_ransom_count:
return False
return TrueRansom Note — அடிக்கடி கேட்கப்படும் கேள்விகள்
Ransom Note பின்னாலுள்ள intuition என்ன?
The note can be built if the magazine has at least as many of each letter as the note needs. Count both and compare.
Ransom Note (LeetCode 383) ஐ எப்படித் தீர்ப்பது?
1. Count letters in both strings. 2. Any letter needed more often than available → False.
Ransom Note இன் time மற்றும் space complexity என்ன?
Time: O(m + n). Space: O(1) — 26 letters. (Accepted approach: Frequency counts.)
Ransom Note இல் பொதுவான தவறுகள் என்ன?
Each magazine letter can be used once.