Problem
GInternational 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:
1 | [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] |
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 1:
1 | Input: words = ["gin", "zen", "gig", "msg"] |
Note:
- The length of
words
will be at most100
. - Each
words[i]
will have length in range[1, 12]
. words[i]
will only consist of lowercase letters.
Analysis
比较简单的字符串题目,题目给出一系列字符串,然后要求我们转换成摩斯密码。由于不同字符的组合最终有可能会产生相同的密码,所以题目要求计算出最后不同的密码数量。我们提前用一个数组存好字符和密码的转换,然后一次遍历就可以转换成密码了。因为是计算不同的密码,使用set来做去重即可。
Solution
无
Code
1 | class Solution { |
Summary
这道题目的分享到这里,谢谢!