Unique Morse Code Words

Omar Ernesto Laria
Strategio
Published in
3 min readFeb 2, 2023

Code challenges play an important role in learning for code engineers and developers. Apart from studying concepts and reading documentation, it is important to apply the knowledge in an environment that simulates real problems. In this way, a developer can get used to the use of the tools that the programming language offers him and when and how to implement them correctly to fulfill his objective.

The following code solves a challenge in Java, which consists of the following:

Given an array of strings words where each word can be written as a concatenation of the Morse code of each letter. For example, “cab” can be written as “-.-.. — …”, which is the concatenation of “-.-.”, “.-”, and “-…”. We will call such a concatenation the transformation of a word. The objective is to return the number of different transformations among all words we have.

To solve this challenge, we use an array of Strings, which contains the 26 letters of the alphabet, but in morse code. The code also requires a HashSet in which we will store the words once we transform them to morse. In order to do that, we implement a ForEach loop that goes through the array of words that the function receives and within this, a For loop is in charge of iterating each letter of the word in which the external loop is positioned. During this inner loop, we use a variable, which we initialize as an empty String. To this, we add each letter in which the internal cycle is found but translated into Morse code. To make the translation we use ASCII, to determine the position of what would be its equivalent in Morse code. Better explained, if the character in which we find ourselves turns out to be a “b”, its equivalent in ASCII would be 98, from this we subtract “a”, which would be 97, and gives us 1 as a result. This means that to the String variable, we will add what is in position ‘1’ of the array that we use as the morse alphabet.

After the inner loop has finished, we add to the set the String variable that contains the morse code word. Following this, the outer loop will iterate again if there are more elements in the array of words.

Because when translating to Morse some words look the same and the challenge asks us to deliver the number of different transformations without there being a repeating pattern, that’s where we take advantage of the HashSet’s property of not storing duplicate values. Since what we must return is an integer value, we create a variable of type Int and equalize it to the size of the HashSet. This should give us the expected number, which we will return.

The time complexity of this code is O(n * m), where n is the number of words in the input and m is the length of each word. The outer loop runs n times, and the inner loop runs m times for each iteration of the outer loop.

The space complexity of this code is O(n * m), as we store all n * m characters in the set. This is the dominant factor in the space complexity, as the space used by the codes and the set data structure is relatively small compared to the size of the words in the input.

In conclusion, the code uses an array of Strings composed of 26 morse code equivalents to each letter of the alphabet in English, uses ASCII code to determine the position of each letter with which it works, and assigns its respective value in morse.

--

--