-
-
Notifications
You must be signed in to change notification settings - Fork 422
/
ransom-note.java
34 lines (27 loc) · 977 Bytes
/
ransom-note.java
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
/**
* Ransom Note
*
* Logic : character count in ransomNote should be equal to OR less than magazine character count.
* Runtime: 7 ms
* Memory Usage: 42.5 MB
*/
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
if (ransomNote.length() > magazine.length())
return false;
/* Logic : character count in ransomNote should be equal to OR less than magazine character count */
int [] ransomNoteChCount = new int[26]; // lower case characters are only 26
for (int i=0;i<ransomNote.length();i++){
ransomNoteChCount[ransomNote.charAt(i) - 'a']++;
}
for (int i=0;i<magazine.length();i++){
ransomNoteChCount[magazine.charAt(i) - 'a']--;
}
for(int i=0;i<26;i++){
if (ransomNoteChCount[i] > 0){
return false;
}
}
return true;
}
}