$ cat ./coding/383-ransom-note.md
[Coding]
383. Ransom Note
────────────────────────────────────────────────────────────
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
counter = Counter(list(magazine))
for ch in ransomNote:
if ch in counter:
if counter[ch] == 0:
return False
counter[ch] -= 1
else:
return False
return True
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
r = Counter(ransomNote)
m = Counter(magazine)
for key, val in r.items():
if key not in m:
return False
if val > m[key]:
return False
return True
時間複雜度 O(n)
空間複雜度 O(n)
--tags#Array#Hash Table
$ ls ./coding/ | grep -v 383-ransom-note