-
-
Notifications
You must be signed in to change notification settings - Fork 610
/
RansomNote.java
34 lines (29 loc) · 1.12 KB
/
RansomNote.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
package problems.easy;
/**
* Created by sherxon on 2016-12-26.
*/
/**
* Given an arbitrary ransom note string and another string containing letters from all the magazines,
* write a function that will return true if the ransom note can be constructed from the magazines ;
* otherwise, it will return false.
* Each letter in the magazine string can only be used once in your ransom not
*/
public class RansomNote {
/**
* The idea is to create an array with length of 26 (English letters) and store each frequency of each letter.
* and remove ransom note letters from array. if certain character count in array is 0, that means ransom note has a
* character that is not included on magazine.
*/
public boolean canConstruct(String ransomNote, String magazine) {
if (ransomNote.length() > magazine.length()) return false;
int[] a = new int[26];
for (char c : magazine.toCharArray()) {
a[c - 'a']++;
}
for (char c : ransomNote.toCharArray()) {
if (a[c - 'a'] == 0) return false;
a[c - 'a']--;
}
return true;
}
}