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.