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, "cba" 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 "--...--.".
Note:
The length of words will be at most 100.
Each words[i] will have length in range [1, 12].
words[i] will only consist of lowercase letters.
Solution:
using System;
using System.Collections.Generic;
using System.Text;
namespace NetCoreCoding.LeetCode.String.Easy
{
public class UniqueMorseRepresentationsSoln
{
public UniqueMorseRepresentationsSoln()
{
}
public void execute()
{
var words = new string[] { "gin", "zen", "gig", "msg" };
var res = UniqueMorseRepresentations(words);
Assert(res, 2);
}
private void Assert(int response, int expected)
{
if (response != expected)
throw new Exception("Incorrect Answer.");
}
public int UniqueMorseRepresentations(string[] words)
{
var morse = new string[] { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." };
var set = new HashSet<string>();
foreach (var word in words)
{
var str = new StringBuilder();
for (int i = 0; i < word.Length; i++)
{
str.Append(morse[word[i]-'a']);
}
set.Add(str.ToString().Trim());
}
return set.Count;
}
}
}
Time Complexity: O(n^2)
Space Complexity: O(n)



