822. Unique Morse Code Words
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to find the number of unique "transformations" from a given list of words. A transformation of a word is defined as the concatenation of the Morse code representations of its individual letters. We are provided with a complete mapping of the 26 lowercase English letters to their Morse codes.
Solution
Unique Morse Code Words in Python
class Solution:
def uniqueMorseRepresentations(self, words: List[str]) -> int:
# Morse code table for letters 'a' through 'z'.
morse = [
".-","-...","-.-.","-..",".","..-.","--.","....","..",".---",
"-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-",
"..-","...-",".--","-..-","-.--","--.."
]
# Use a set to store unique transformations.
transformations = set()
# Iterate over each word in the input list.
for word in words:
transformation_parts = []
# For each character in the word, find its Morse code.
for char in word:
# ord(char) - ord('a') gives the 0-based index for the morse table.
transformation_parts.append(morse[ord(char) - ord('a')])
# Join the parts to form the complete transformation string.
full_transformation = "".join(transformation_parts)
# Add the transformation to the set. Duplicates are ignored.
transformations.add(full_transformation)
# The length of the set is the number of unique transformations.
return len(transformations)
# A more concise, "Pythonic" way to write the same logic:
# morse_map = [ ... ]
# unique_transformations = {"".join(morse_map[ord(c) - ord('a')] for c in word) for word in words}
# return len(unique_transformations)
Unique Morse Code Words in Java
class Solution {
public int uniqueMorseRepresentations(String[] words) {
// Morse code table for letters 'a' through 'z'.
String[] morse = {
".-","-...","-.-.","-..",".","..-.","--.","....","..",".---",
"-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-",
"..-","...-",".--","-..-","-.--","--.."
};
// Use a HashSet to store unique transformations.
Set<String> transformations = new HashSet<>();
// Iterate over each word in the input array.
for (String word : words) {
// Use StringBuilder for efficient string concatenation.
StringBuilder sb = new StringBuilder();
// For each character in the word, append its Morse code.
for (char c : word.toCharArray()) {
// c - 'a' gives the 0-based index for the morse table.
sb.append(morse[c - 'a']);
}
// Add the complete transformation to the set.
// The set automatically handles duplicates.
transformations.add(sb.toString());
}
// The size of the set is the number of unique transformations.
return transformations.size();
}
}
Unique Morse Code Words in C++
class Solution {
public:
int uniqueMorseRepresentations(vector<string>& words) {
// Morse code table for letters 'a' through 'z'.
vector<string> morse = {
".-","-...","-.-.","-..",".","..-.","--.","....","..",".---",
"-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-",
"..-","...-",".--","-..-","-.--","--.."
};
// Use an unordered_set to store unique transformations.
unordered_set<string> transformations;
// Iterate over each word in the input list.
for (const string& word : words) {
string transformation = "";
// For each character in the word, append its Morse code.
for (char c : word) {
// 'c' - 'a' gives the 0-based index for the morse table.
transformation += morse[c - 'a'];
}
// Insert the complete transformation into the set.
// The set automatically handles duplicates.
transformations.insert(transformation);
}
// The size of the set is the number of unique transformations.
return transformations.size();
}
};
Unique Morse Code Words in TypeScript
function uniqueMorseRepresentations(words: string[]): number {
// Morse code table for letters 'a' through 'z'.
const morse = [
".-","-...","-.-.","-..",".","..-.","--.","....","..",".---",
"-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-",
"..-","...-",".--","-..-","-.--","--.."
];
// Use a Set to store unique transformations.
const transformations = new Set<string>();
// Iterate over each word in the input array.
for (const word of words) {
let transformation = '';
// For each character in the word, append its Morse code.
for (const char of word) {
// char.charCodeAt(0) - 'a'.charCodeAt(0) gives the 0-based index.
transformation += morse[char.charCodeAt(0) - 'a'.charCodeAt(0)];
}
// Add the complete transformation to the set.
// The set automatically handles duplicates.
transformations.add(transformation);
}
// The size of the set is the number of unique transformations.
return transformations.size;
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.