-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRansom Note.java
More file actions
40 lines (30 loc) · 848 Bytes
/
Ransom Note.java
File metadata and controls
40 lines (30 loc) · 848 Bytes
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
40
class Solution {
/**
* Time: O(n+m)
* Memory: O(1)
*/
public boolean canConstruct(String ransomNote, String magazine) {
int[] arr = new int[26];
for (int i = 0; i < magazine.length(); ++i)
arr[magazine.charAt(i) - 'a']++;
for (int i = 0; i < ransomNote.length(); ++i)
if (--arr[ransomNote.charAt(i) - 'a'] < 0)
return false;
return true;
}
}
class Solution {
/**
* Time: O(n+m)
* Memory: O(1)
*/
public boolean canConstruct(String ransomNote, String magazine) {
int[] arr = new int[26];
for (char c : magazine.toCharArray())
arr[c - 'a']++;
for (char c : ransomNote.toCharArray())
if (--arr[c - 'a'] < 0)
return false;
return true;
}
}