Unique Morse Code Words, by LeetCode
Problem tonight is this one: https://leetcode.com/problems/unique-morse-code-words/description/
Relatively simple: parse the words, parse the letters, build the "key" based on the code, check against a hash table, store it, return the count in the hash table. Code is below, hugs & hugs!! Marcelo
public class Solution
{
public int UniqueMorseRepresentations(string[] words)
{
string[] code = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." };
Hashtable ht = new Hashtable();
foreach (string w in words)
{
string key = "";
foreach (char c in w)
{
key += code[c - 'a'].ToString();
}
if (!ht.ContainsKey(key)) ht.Add(key, true);
}
return ht.Count;
}
}
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:
[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
Now, given a list of words, 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 "-.-." + "-..." + ".-"). We'll call such a concatenation, the transformation of a word.
Return the number of different transformations among all words we have.
Example: 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 "--...--.".
Relatively simple: parse the words, parse the letters, build the "key" based on the code, check against a hash table, store it, return the count in the hash table. Code is below, hugs & hugs!! Marcelo
public class Solution
{
public int UniqueMorseRepresentations(string[] words)
{
string[] code = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." };
Hashtable ht = new Hashtable();
foreach (string w in words)
{
string key = "";
foreach (char c in w)
{
key += code[c - 'a'].ToString();
}
if (!ht.ContainsKey(key)) ht.Add(key, true);
}
return ht.Count;
}
}
Very clean solution, Marcelo! Python solution:
ReplyDeleteclass Solution:
def uniqueMorseRepresentations(self, words):
"""
:type words: List[str]
:rtype: int
"""
codes = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
return len(set(["".join([codes[ord(letter) - ord("a")] for letter in word]) for word in words]))
Or a more readable version:
class Solution:
def uniqueMorseRepresentations(self, words):
"""
:type words: List[str]
:rtype: int
"""
codes = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
encodeLetter = lambda letter: codes[ord(letter) - ord("a")]
encodeWord = lambda word: "".join(map(encodeLetter, word))
return len(set(map(encodeWord, words)))
Cheers,
Taras