A kidnapper wrote a ransom note but is worried it will be traced back to him. He found a magazine and wants to know if he can cut out whole words from it and use them to create an untraceable replica of his ransom note. The words in his note are case-sensitive and he must use whole words available in the magazine, meaning he cannotuse substrings or concatenation to create the words he needs.
Given the words in the magazine and the words in the ransom note, print Yes
if he can replicate his ransom note exactly using whole words from the magazine; otherwise, print No
.
Input Format
The first line contains two space-separated integers describing the respective values of (the number of words in the magazine) and (the number of words in the ransom note).
The second line contains space-separated strings denoting the words present in the magazine.
The third line contains space-separated strings denoting the words present in the ransom note.
Constraints
- .
- Each word consists of English alphabetic letters (i.e., to and to ).
- The words in the note and magazine are case-sensitive.
Output Format
Print Yes
if he can use the magazine to create an untraceable replica of his ransom note; otherwise, print No
.
Sample Input 0
6 4
give me one grand today night
give one grand today
Sample Output 0
Yes
Sample Input 1
6 5
two times three is not four
two times two is four
Sample Output 1
No
풀이전, 미리 입력되어 있는 변수나 메소드를 그대로 사용해야지만 인식하는 줄 알았는데
다른 사람들의 풀이를 보니 꼭 그럴 필요는 없는 듯...
애초에 map이 아니라 hashtable 로 해도 되고 solve 메소드도 그렇고 noteMap은 필요없다고 생각했는데
일단 깔끔하지는 않지만 모든 케이스를 한번에 통과해서 바로 코드 올림
풀이
import java.util.*;
public class Solution {
Map<String, Integer> magazineMap;
Map<String, Integer> noteMap;
boolean isRansome = false;
public Solution(String magazine, String note) {
magazineMap = new HashMap();
noteMap = new HashMap();
String [] magazines = magazine.split(" ");
for (String s : magazines) {
if (!magazineMap.containsKey(String.valueOf(s))) {
magazineMap.put(String.valueOf(s), 1);
} else {
int count = magazineMap.get(String.valueOf(s)) + 1;
magazineMap.put(String.valueOf(s), count);
}
}
String [] notes = note.split(" ");
for (String s : notes){
if (magazineMap.containsKey(s)){
int count = magazineMap.get(s);
if (count > 0) {
count -=1;
magazineMap.put(s, count);
isRansome = true;
} else {
isRansome = false;
break;
}
} else {
isRansome = false;
break;
}
}
}
public boolean solve() {
return isRansome;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt();
int n = scanner.nextInt();
// Eat whitespace to beginning of next line
scanner.nextLine();
Solution s = new Solution(scanner.nextLine(), scanner.nextLine());
scanner.close();
boolean answer = s.solve();
if(answer)
System.out.println("Yes");
else System.out.println("No");
}
}
'Algorithms > Cracking the Coding Interview' 카테고리의 다른 글
Queues: A Tale of Two Stacks (0) | 2018.02.20 |
---|---|
Stacks: Balanced Brackets (0) | 2018.02.19 |
Linked Lists: Detect a Cycle (0) | 2018.02.19 |
Strings: Making Anagrams (0) | 2018.02.09 |
Arrays: Left Rotation (0) | 2018.02.08 |