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.