Appreciating Scala’s Conciseness

Recently, I have been solving algorithm problems on HackerRank. After solving a problem, I go through solutions of other people to see what I can glean from that. Occasionally, I also look at solutions from other Nigerians (maybe because I am patriotic 😃).

After solving a problem with Scala, I checked the solutions of people using other languages and noticed an interesting thing; the solution in Scala was about a few lines of code while in other languages, it was HUGE.

Though, to be fair, from the way the problem is introduced and the accompanying tutorial it seems Hashtables is the expected solution and most of the solutions in other languages try to use Hashtables. Notwithstanding, a solution using Hashtables (Maps) in Scala is concise when compared to the other languages.

The Problem:

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 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.

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.

Output Format

Print Yes if he can use the magazine to create an untraceable replica of his ransom note; otherwise, print No.

Sample Input

Sample Output

The Solutions

Scala

or

Python

Solution by Zaga

Java

Solution by ADEGEYE MAYOWA

Why

The solutions are concise in Scala because of its impressive collection methods. Scala provides an impressive list of methods when working with its collections. For most languages, you would have to use third party libraries or define yours.

Talks to Computers

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store