International Morse Code defines a standard encoding where all letter are mapped to a series of dots and dashes, as follow S: Maps to, maps to, "a"
".-"
"b"
"-..."
"c"
maps "-.-."
to, and so on.
For convenience, the full table for the letters of the Chinese 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, the ' 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:2explanation:the transformation of each word is: "Gin"- -...-."" Zen "--...-." " Gig "--...--." " Msg ","--...--. " There is 2 different transformations, "--...-." and "--...--.".
1 //Time o (n), Space O (n) HashSet to remove duplicates2 3 Public intuniquemorserepresentations (string[] words) {4string[] Map =Newstring[]{".-", "-...", "-.-.", "-..", ".", "... -.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-",".. -","...-",".--","-.. -","-.--","--.."};5 6 if(Words = =NULL|| Words.length = = 0) {7 return0;8 }9 Tenhashset<string> result =NewHashset<string>(); One A for(String word:words) { -StringBuilder SB =NewStringBuilder (); - the for(inti = 0; I < word.length (); i++) { - int loc = Word.charat (i)-' a '; - sb.append (Map[loc]); - } + - Result.add (sb.tostring ()); + } A at returnresult.size (); -}
804. Unique Morse Code Words