Morse Code To English

Easy
0/40
Average time to solve is 15m
14 upvotes
Asked in companies
AdobeAppleGoldman Sachs

Problem statement

You are given a string of length N representing the morse code(s). You have to convert this code into the corresponding alphanumeric code containing small case english alphabet[a-z] and digits[0-9].

For your convenience, the full table for the 26 letters of the English alphabet and 10 numeric digits is given below:

list = [ ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----." ]      

where list[0] represents morse code for ‘a’, list[1] represents morse code for ‘b’, and list[35] represents morse code for ‘9’. Similarly, each letter and numeric is mapped with a morse code given in the list.               
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then the test cases follow.

The first line of each test case contains the morse code in the form of a string.
Output format :
For each test case, print the equivalent alphanumeric code of the given morse code in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N <= 5*(10^5)

Time Limit: 1sec
Sample Input 1 :
2
-.-. --- -.. .. -. --.
-. .. -. .--- .- ...
Sample Output 1 :
coding
ninjas
Explanation For Sample Input 1 :
In the first test case, On splitting the given string on the basis of space the list we get is :  [ "-.-.", "---", "-..", "..",  "-.", "--." ] where “-.-.” is morse code for ‘c’ and similarly further in the string.

In the second case, On splitting the given string on the basis of space the list we get is : [ "-.", "..", "-.", ".---", ".-", "..." ], where “-.” is morse code for ‘n’ and similarly further in the string.
Sample Input 2 :
2
----. ---.. ....- ..... ----- 
-.. .... --- -. .. --... --...
Sample Output 2 :
98450
dhoni77
Hint

Try to store the character and its morse code equivalent in pairs.

Approaches (1)
Morse Code To English
  • The idea to approach the problem is to store the character and its morse code as pairs. Make a HashMap which contains key-value pairs (where key = MorseCode and value = Character Equivalent).
  • Split the given morse code on the basis of spaces and store them in a list.
  • Traverse the list and store the equivalent character element by element in an empty string named ans. (For java we will use StringBuffer for optimized string operations).
  • Return string ans as our answer.
Time Complexity

O(N), where N is the length of the morse code string.

 

As you need to traverse the list once after splitting the given morse code.

Space Complexity

O(N), where N is the length of the morse code string.

 

As HashMap is of maximum N size.

Code Solution
(100% EXP penalty)
Morse Code To English
Full screen
Console