Java Solution for Hash Tables: Ransom Note HackerRank Problem
Problem Description :-
Harold is a kidnapper who wrote a ransom note, but now he is worried it will be traced back to him through his handwriting. 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 only whole words available in the magazine. He cannot use 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.
For example, the note is "Attack at dawn". The magazine contains only "attack at dawn". The magazine has all the right words, but there's a case mismatch. The answer is No.
Function Description
Complete the checkMagazine function in the editor below. It must print Yes if the note can be formed using the magazine, or No.
checkMagazine has the following parameters:
- magazine: an array of strings, each a word in the magazine
- note: an array of strings, each a word in the ransom note
See full description in HackerRank :-
Let's see solution :-
Solution 1 :-
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class HashTablesRansomNote {
static void checkMagazine(String[] magazine, String[] note) {
Map<String, Integer> map = new HashMap<>();
// Put all note words into map and total occurrence
for (String string : note) {
if (!map.containsKey(string)) {
map.put(string, 1);
} else {
map.put(string, (map.get(string)+1));
}
}
// Remove founded word from map
for (String string : magazine) {
if (map.containsKey(string)) {
map.put(string, (map.get(string)-1));
if (map.get(string) == 0) {
map.remove(string);
}
}
}
// If map contains any value that means
// some words are missing in magazine
if (map.size() > 0) {
System.out.println("No");
} else {
System.out.println("Yes");
}
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
String[] mn = scanner.nextLine().split(" ");
int m = Integer.parseInt(mn[0]);
int n = Integer.parseInt(mn[1]);
String[] magazine = new String[m];
String[] magazineItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int i = 0; i < m; i++) {
String magazineItem = magazineItems[i];
magazine[i] = magazineItem;
}
String[] note = new String[n];
String[] noteItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int i = 0; i < n; i++) {
String noteItem = noteItems[i];
note[i] = noteItem;
}
checkMagazine(magazine, note);
scanner.close();
}
}
Explanation :-
- First we loop through note and put all words with words occurrence. key as word and value as total word occurrence.
- Loop through magazine and check if note's word is present in magazine array or not. if note's word found in magazine array then we delete that from map.
- Now last, if map contains any value in it that means magazine missing words that contains into note.
Solution 2 :-
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class HashTablesRansomNote {
static void checkMagazine(String[] magazine, String[] note) {
Arrays.sort(magazine);
Arrays.sort(note);
List<String> magazineList = new ArrayList<>();
Collections.addAll(magazineList, magazine);
List<String> noteList = new ArrayList<>();
Collections.addAll(noteList, note);
boolean flag = false;
for (int i = 0; i < noteList.size(); i++) {
if (magazineList.contains(note[i])) {
magazineList.remove(note[i]);
flag = true;
} else {
flag = false;
break;
}
}
System.out.println(flag ? "Yes" : "No");
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
String[] mn = scanner.nextLine().split(" ");
int m = Integer.parseInt(mn[0]);
int n = Integer.parseInt(mn[1]);
String[] magazine = new String[m];
String[] magazineItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int i = 0; i < m; i++) {
String magazineItem = magazineItems[i];
magazine[i] = magazineItem;
}
String[] note = new String[n];
String[] noteItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int i = 0; i < n; i++) {
String noteItem = noteItems[i];
note[i] = noteItem;
}
checkMagazine(magazine, note);
scanner.close();
}
}
Explanation :-
- First we sort both arrays.
- Create two new ArrayList and store both arrays into them.
- We loop through Note List array and if magazine list contains note list word then we delete magazine array founded word. and set flag to true.
- if any word does not found in magazine then set flag as false and break the loop. no need to check after that.
If you have any query regarding to above code or explanation then comment down.
Other HackerRank problem and solutions :-
Spring boot Tutorials :-
- Spring Boot Crud Operation with Thymeleaf + MySql + JPA
- Spring Boot REST full API CRUD operation with MySql
Comments
Post a Comment