Looking for good programming challenges?

Use the search below to find our solutions for selected questions!

Ransom Note Challenge

Sharing is caring!

Problem Statement
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

Write a function checkMagazine that prints 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

Solution

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    static Map<String, Integer> createMapWithWordCount(String[] words) {
        Map<String, Integer> word2Count = new HashMap<String, Integer>();
        
        for (String word:words) {
            if (word2Count.containsKey(word)) {
                int count = word2Count.get(word);
                word2Count.put(word, count+1);
            } else {
                 word2Count.put(word, 1);
            }
        }

        return word2Count;
    }

    // Complete the checkMagazine function below.
    static void checkMagazine(String[] magazine, String[] note) {
        Map<String, Integer> magazineWord2Count = createMapWithWordCount(magazine);

        for (String word:note) {
            if (magazineWord2Count.containsKey(word) && magazineWord2Count.get(word)>0) {
                int count = magazineWord2Count.get(word);
                magazineWord2Count.put(word, count-1);
            } else {
                System.out.println("No");
                return;
            }
        }

        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();
    }
}