-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRansomNote.java
More file actions
107 lines (58 loc) · 2.43 KB
/
RansomNote.java
File metadata and controls
107 lines (58 loc) · 2.43 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Dell
*/
import java.io.*;
import java.util.*;
public class RansomNote {
public static String checkRansom(String[] magWord,String[] noteWord){
HashMap<String ,Integer> magazine = new HashMap<String , Integer>();
//inserting each word in the magazine map and along with its counter value in the string
for(String m : magWord){
if(magazine.containsKey(m)){
magazine.put(m,magazine.get(m) + 1);
}
else{
magazine.put(m, 1);
}
}
//looping over noteword array and finding if magazine contains that word or not
for(String n : noteWord){
//if not the we simply return a no.
if(!magazine.containsKey(n)){
return "No";
}
//if yes then we adjust counter to the frequecy of word - 1
int counter = magazine.get(n) - 1;
if(counter == 0){
magazine.remove(n);
}
// now we put back again that word along with its updated frequency into the array.
else{
magazine.put(n,counter);
}
}
return "Yes";
}
public static void main(String[] args) throws IOException{
// BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc.nextInt();
String[] mag = new String[m];
String[] note = new String[n];
for(int i = 0 ; i < m ; i++){
mag[i] = sc.next();
}
for(int j = 0 ; j < n ; j++){
note[j] = sc.next();
}
String str = checkRansom(mag,note);
System.out.println(str);
}
}