LeetCode: Ransom Note

时间:2021-12-26 03:57:07
 public class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] ransomNum = new int[256];
int[] magNum = new int[256];
for (int i = 0; i < 256; i++) {
ransomNum[i] = magNum[i] = 0;
}
for (int i = 0; i < ransomNote.length(); i++) {
ransomNum[(int)ransomNote.charAt(i)]++;
}
for (int i = 0; i < magazine.length(); i++) {
magNum[(int)magazine.charAt(i)]++;
}
for (int i = 0; i < 256; i++) {
if (ransomNum[i] > magNum[i]) return false;
}
return true;
}
}