[
string
counter
]
Leetcode 0383 Ransom Note
Problem statement
https://leetcode.com/problems/ransom-note/
Solution
Just count letters in both strings, for example using counters and check if we have enough letters. It can be done in one line as well.
Complexity
Time complexity is O(m+n)
and space complexity is O(26)
.
Code
class Solution:
def canConstruct(self, ransomNote, magazine):
return not Counter(ransomNote) - Counter(magazine)