383. Ransom Note#
1public boolean canConstruct(String ransomNote, String magazine) {
2 int[] count = new int[26];
3
4 for (char c : magazine.toCharArray()) {
5 count[c - 'a']++;
6 }
7
8 for (char c : ransomNote.toCharArray()) {
9 if (count[c - 'a'] == 0) {
10 return false;
11 }
12 count[c - 'a']--;
13 }
14
15 for (int i : count) {
16 if (i < 0) return false;
17 }
18
19 return true;
20}