-
Notifications
You must be signed in to change notification settings - Fork 0
/
ransomNote.java
39 lines (34 loc) · 1.07 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
35
36
37
38
39
package week2;
/***
* Given two strings ransomNote and magazine,
* return true if ransomNote can be constructed by using the letters from magazine and false otherwise.
*
* Each letter in magazine can only be used once in ransomNote.
*
* Example 1:
*
* Input: ransomNote = "a", magazine = "b"
* Output: false
* Example 2:
*
* Input: ransomNote = "aa", magazine = "ab"
* Output: false
*/
public class ransomNote {
public static void main(String[] args) {
String ransomNote = "a", magazine = "b";
System.out.println(canConstruct(ransomNote, magazine));
}
public static boolean canConstruct(String ransomNote, String magazine) {
if (ransomNote.length() > magazine.length()) return false;
int[] index = new int[26];
for (int i = 0; i < magazine.length(); i++) {
index[magazine.charAt(i) - 'a']++;
}
for (int i = 0; i < ransomNote.length(); i++) {
if (index[ransomNote.charAt(i) - 'a'] == 0) return false;
index[ransomNote.charAt(i) - 'a']--;
}
return true;
}
}