804. Unique Morse Code Words LeetCode Solution
In this guide, you will get 804. Unique Morse Code Words LeetCode Solution with the best time and space complexity. The solution to Unique Morse Code Words problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Unique Morse Code Words solution in C++
- Unique Morse Code Words solution in Java
- Unique Morse Code Words solution in Python
- Additional Resources

Problem Statement of Unique Morse Code Words
International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows:
‘a’ maps to “.-“,
‘b’ maps to “-…”,
‘c’ maps to “-.-.”, and so on.
For convenience, the full table for the 26 letters of the English alphabet is given below:
[“.-“,”-…”,”-.-.”,”-..”,”.”,”..-.”,”–.”,”….”,”..”,”.—“,”-.-“,”.-..”,”–“,”-.”,”—“,”.–.”,”–.-“,”.-.”,”…”,”-“,”..-“,”…-“,”.–“,”-..-“,”-.–“,”–..”]
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.
Return the number of different transformations among all words we have.
Example 1:
Input: words = [“gin”,”zen”,”gig”,”msg”]
Output: 2
Explanation: The transformation of each word is:
“gin” -> “–…-.”
“zen” -> “–…-.”
“gig” -> “–…–.”
“msg” -> “–…–.”
There are 2 different transformations: “–…-.” and “–…–.”.
Example 2:
Input: words = [“a”]
Output: 1
Constraints:
1 <= words.length <= 100
1 <= words[i].length <= 12
words[i] consists of lowercase English letters.
Complexity Analysis
- Time Complexity: O(\Sigma |\texttt{words[i]}|)
- Space Complexity: O(|\texttt{words}|)
804. Unique Morse Code Words LeetCode Solution in C++
class Solution {
public:
int uniqueMorseRepresentations(vector<string>& words) {
const vector<string> morse{
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
unordered_set<string> transformations;
for (const string& word : words) {
string transformation;
for (const char c : word)
transformation += morse[c - 'a'];
transformations.insert(transformation);
}
return transformations.size();
}
};
/* code provided by PROGIEZ */
804. Unique Morse Code Words LeetCode Solution in Java
class Solution {
public int uniqueMorseRepresentations(String[] words) {
final String[] morse = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
Set<String> transformations = new HashSet<>();
for (final String word : words) {
StringBuilder transformation = new StringBuilder();
for (final char c : word.toCharArray())
transformation.append(morse[c - 'a']);
transformations.add(transformation.toString());
}
return transformations.size();
}
}
// code provided by PROGIEZ
804. Unique Morse Code Words LeetCode Solution in Python
class Solution:
def uniqueMorseRepresentations(self, words: list[str]) -> int:
morse = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."]
transformations = set()
for word in words:
transformation = ''.join(
morse[string.ascii_lowercase.index(c)] for c in word)
transformations.add(transformation)
return len(transformations)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.