TreeviewCopyright © aleen42 all right reserved, powered by aleen42
383. 赎金信
https://leetcode-cn.com/problems/ransom-note/
Java
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] countR = new int[26];
int[] countM = new int[26];
for(char c : ransomNote.toCharArray()) countR[c - 'a'] ++;
for(char c : magazine.toCharArray()) countM[c - 'a'] ++;
for(int i = 0; i < 26; i++) {
if(countR[i] > countM[i]) {
return false;
}
}
return true;
}
}
Python
'''
Author: Goog Tech
Date: 2020-08-24 16:26:37
LastEditTime: 2020-08-24 16:28:45
Description: https://leetcode-cn.com/problems/ransom-note/
FilePath: \leetcode-googtech\#383. Ransom Note\Solution.py
WebSite: https://algorithm.show/
'''
class Solution(object):
def canConstruct(self, ransomNote, magazine):
"""
:type ransomNote: str
:type magazine: str
:rtype: bool
"""
for ch in set(ransomNote):
if ransomNote.count(ch) > magazine.count(ch):
return False
return True