-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday3.java
More file actions
37 lines (36 loc) · 1018 Bytes
/
day3.java
File metadata and controls
37 lines (36 loc) · 1018 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
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int i=0,j=0,k=0;
boolean b=true;
char[] rn=ransomNote.toCharArray();
char[] mag=magazine.toCharArray();
Arrays.sort(rn);
Arrays.sort(mag);
HashMap<Character,Integer> hm=new HashMap<Character,Integer>();
for(i=0;i<mag.length;i++){
if(!hm.containsKey(mag[i])){
hm.put(mag[i],1);
}
else{
j=hm.get(mag[i]);
hm.put(mag[i],j+1);
}
}
for(i=0;i<rn.length;i++){
if(!hm.containsKey(rn[i])){
b=false;
break;
}
else{
j=hm.get(rn[i]);
if(j<=0){
b=false;
break;
}
hm.put(rn[i],j-1);
}
b=true;
}
return b;
}
}