-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path383. Ransom Note.java
More file actions
36 lines (36 loc) · 1.05 KB
/
Copy path383. Ransom Note.java
File metadata and controls
36 lines (36 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
if(ransomNote.length() == 0){
return true;
}
if(ransomNote.length() != 0 && magazine.length() == 0){
return false;
}
char[] magChar = magazine.toCharArray();
char[] ransomNoteChar = ransomNote.toCharArray();
int[] magCharCount = new int[26];
for (int i = 0; i < magCharCount.length; i++) {
magCharCount[i] = 0;
}
for (int i = 0; i < magChar.length; i++) {
magCharCount[magChar[i]-'a'] += 1;
}
// check if it is possible.
for (int i = 0; i < ransomNoteChar.length; i++) {
int index = ransomNoteChar[i]-'a';
magCharCount[index]--;
if(magCharCount[index] < 0)
{
return false;
}
}
return true;
}
}