Contents

Morse Code 1 (with.Java)

   Feb 5, 2024     4 min read

This is an article about the “Morse Code 1” problem.

As I solve coding test problems, I look back on the problems I solved and look into different solution methods to learn more.

Let’s look at the problem first.

problem

I received a letter using Morse code from a shy friend.

Since I can’t read it, I’m trying to create a program to decipher it.

When the string letter is given as a parameter, complete the solution function to return a string with letter changed to lowercase English.

Morse code is as follows:

morse = {
'.-':'a','-...':'b','-.-.':'c','-..':'d','.':'e',' ..-.':'f',
'--.':'g','....':'h','..':'i','.---':'j','-.-':'k' ,'.-..':'l',
'--':'m','-.':'n','---':'o','.--.':'p','--.-':'q', '.-.':'r',
'...':'s','-':'t','..-':'u','...-':'v','.--':'w',' -..-':'x',
'-.--':'y','--..':'z'
}

Restrictions

  • 1 ≤ letter length ≤ 1,000
  • The return value is lowercase.
  • Morse code for letters is separated by spaces.
  • There cannot be more than two consecutive spaces in a letter.
  • Indecipherable letters will not be given.
  • There are no spaces at the beginning and end of the letter.

Input/Output Example

letterresult
“…. .-.. .-.. —”“hello”
”.–. -.– - …. — -.”“python”

My solution to the problem

class Solution {
     public String solution(String letter) {
         String[] morseArray = letter.split(" ");
         StringBuilder answer = new StringBuilder();
         String[] morseAlphabet = {
             ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", " ..", ".---",
             "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", " ...", "-",
             "..-", "...-", ".--", "-..-", "-.--", "--.."
         };

         for (String morse : morseArray){
             for (int i = 0; i < morseAlphabet.length; i++) {
                 if (morse.equals(morseAlphabet[i])) {
                     char decodedChar = (char) ('a' + i);
                     answer.append(decodedChar);
                 }
             }
         }

         return answer.toString();
     }
}

Solution explanation

split: Creates an array by dividing the string based on spaces.

Here, the Morse code is divided by space and stored as an array.

(char) (‘a’ + i): Converting the value of i to alphabet using ASCII code. ‘a’ + i returns ‘a’ when i is 0, returns ‘b’ when i is 1, and so on.

Why (char) (‘a’ + i) It is used to obtain the alphabet in order from ‘a’ according to the i value.

(char) means casting to convert an integer to a character, and ‘a’ + i is an expression to get the alphabet corresponding to i in the ASCII code.

With this, I’m converting the Morse code to an alphabet and building the result into a final string using StringBuilder.

In Java, you can use the index of the array to map each value directly.

However, if you need mapping for multiple reasons, it may be convenient to use HashMap or another mapping structure.

When using HashMap

  • Dynamic mapping: When new alphabets or Morse codes may be added or changed, HashMap allows you to dynamically adjust the mapping.
  • Key-value pair management: HashMap allows you to easily add, delete, and modify key and value pairs.

example:

// Initialize HashMap using initialization block
     static {
         morseMap = new HashMap<>();
         morseMap.put(".-", 'a');
         morseMap.put("-...", 'b');
         morseMap.put("-.-.", 'c');
         ...
      }

When using arrays

  • Static mapping: Arrays can be used when the mapping is fixed and does not change.
  • Simple structure: When the mapping is simple and unlikely to change, you can use arrays to keep your code concise.

Therefore, it is important to choose the appropriate data structure depending on the situation. Just write code considering the pros and cons of each.