# Check if given Morse Code is Valid

In this problem, we will validate the Morse code. The Morse code method was used to transform the text in the encoded format, and it was very helpful to provide communication between two entities before the telephone was invented.

There are standard codes for each alphabetical character containing the ‘.’ And ‘−‘. The code is prepared based on the transmission time, where ‘.’ Represents the short signals, and ‘−‘ represents the long signals.

However, in this tutorial, we will use the standardized Morse code to decode and validate the string.

Here is the table of standardized Morse code.

 A ".−" H "...." O "−−−" V "...−" B "−...", I ".." P ".−−." W ".−−" C "−.−." J ".−−−" Q "−−.−" X "−..−" D "−.." K "−.−" R ".−." Y "−.−−" E "." L ".−.." S "..." Z "−−.." F "..−." M "−−" T "−" G "−−." N "−." U "..−"

Problem statement − We have given a string containing Morse code. We need to check whether the Morse code is valid.

Sample examples

Input

morseCode = "− ..− − −−− .−. .. .−.. ... .−−. −−− .. −. −";


Output

Yes


Explanation − We get the ‘TUTORIALSPOINT’ when we decode the string. So, the Morse code is valid.

Input

morseCode = "−− .−.−.−− −−−−−−";


Output

No


Explanation − The Morse code is not valid.

Input

morseCode = "*− .−.−.−− −−−−−−";


Output

No


Explanation − The Morse code contains the ‘*’ character. So, it is invalid.

## Approach 1

In this approach, we will use the map data structure. We will store the Morse code for each alphabetical character as a key in the map and the alphabetical character as a value.

The given string contains the space−separated Morse code for each character. So, we will take each word of the string and check whether it is present as a key in the map. If any single word is absent from the map, we can say that the Morse code is invalid.

### Algorithm

Step 1 − Initialize the ‘codeMap’ containing the Morse code as a key and character as a value.

Step 2 − Initialize the ‘tmp’ string to store the current word of the string.

Step 3 − Start traversing each character of the given string.

Step 4 − If the current character is space, check whether the ‘tmp’ string value exists in the map as a key. If it does not exist, return false. Otherwise, re−initialize the ‘tmp’ string.

Step 5 − For the ‘.’ and ‘−‘ characters, append the character to the ‘tmp’ string.

Step 6 − Return true at the end of the function.

### Example

#include <bits/stdc++.h>
using namespace std;

bool validateMorse(string mCode){
map<string, char> codeMap{
{".-", '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'}, {"-----", '0'},
{".----", '1'}, {"..---", '2'}, {"...--", '3'}, {"....-", '4'}, {".....", '5'}, {"-....", '6'},
{"--...", '7'}, {"---..", '8'}, {"----.", '9'}
};
string tmp = "";
for (char ch : mCode) {
if (ch == ' ') {
// Validate morse code
if (codeMap.find(tmp) == codeMap.end()) {
return false;
}
tmp = ""; // Reset tmp string
} else {
tmp += ch;
}
}
return true;
}
int main() {
string morseCode = "- ..- - --- .-. .. .-.. ... .--. --- .. -. -";
if (validateMorse(morseCode)) {
cout << "The Morse code is valid." << endl;
} else {
cout << "The Morse code is not valid." << endl;
}
return 0;
}



### Output

The Morse code is valid.


Time complexity − O(N) for traversing the string.

Space complexity − O(1) as we use the constant space to store the Morse code.

We learned to validate the Morse code. Programmers may split the string using the space delimiter to get all words of the string. Each word of the string represents the alphabetical character, and we can check whether the word represents the Morse code for any character.

Updated on: 17-Jul-2023

65 Views