Leetcode 383. Ransom Note | Hindi | Python | Leetcode | Optimized way

Опубликовано: 30 Ноябрь 2025
на канале: DataEngineering360
44
0

#leetcode #python #timecomplexity
Q. Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using the letters from magazine and false otherwise.

Each letter in magazine can only be used once in ransomNote.



Example 1:

Input: ransomNote = "a", magazine = "b"
Output: false

Solution -
Optimized way - Note - this is sample code.. we cannot use angle brackets in description, so I removed them from the code.

class Solution:
def canConstruct(self, ransomNote: str, magazine: str) - bool:
d = {}
t = ransomNote
s = magazine
for i in s:
if i not in d:
d[i] = 1
else:
d[i] += 1
for j in t:
if j not in d:
return False
d[j] -= 1
if d[j] == -1:
return False
return True


#python #leetcodesolutions #dsa #pythoninterviewquestions

Python Developer Interview Questions Answers
Top 10 Python Developer Interview Questions Answer
Mostly asked Python Developer Interview Questions Answer
Commonly asked Python Developer Interview Questions Answer



Tags -
Python leetcode problems
leetcode two sum
leetcode solutions
leetcode hindi
two sum python
two sum problem


На этой странице сайта вы можете посмотреть видео онлайн Leetcode 383. Ransom Note | Hindi | Python | Leetcode | Optimized way длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь DataEngineering360 30 Ноябрь 2025, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 44 раз и оно понравилось 0 зрителям. Приятного просмотра!