[LeetCode] 804. Unique Morse Code Words 独特的摩斯码单词
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 "--...--.".
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.
给定了26字母的摩斯电码的编码,给一组单词,把每个单词都转成摩斯码,返回有多少个不同的摩斯码。
解法:题目很简单,直接转换判断即可。
Java:
1 2 3 4 5 6 7 8 9 10 | public int uniqueMorseRepresentations(String[] words) { String[] d = { ".-" , "-..." , "-.-." , "-.." , "." , "..-." , "--." , "...." , ".." , ".---" , "-.-" , ".-.." , "--" , "-." , "---" , ".--." , "--.-" , ".-." , "..." , "-" , "..-" , "...-" , ".--" , "-..-" , "-.--" , "--.." }; HashSet<String> s = new HashSet<>(); for (String word : words) { String code = "" ; for ( char c : word.toCharArray()) code += d[c - 'a' ]; s.add(code); } return s.size(); } |
Python:
1 2 3 4 | def uniqueMorseRepresentations( self , words): d = [ ".-" , "-..." , "-.-." , "-.." , "." , "..-." , "--." , "...." , ".." , ".---" , "-.-" , ".-.." , "--" , "-." , "---" , ".--." , "--.-" , ".-." , "..." , "-" , "..-" , "...-" , ".--" , "-..-" , "-.--" , "--.." ] return len ({' '.join(d[ord(i) - ord(' a')] for i in w) for w in words}) |
Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | # Time: O(n), n is the sume of all word lengths # Space: O(n) class Solution( object ): def uniqueMorseRepresentations( self , words): """ :type words: List[str] :rtype: int """ MORSE = [ ".-" , "-..." , "-.-." , "-.." , "." , "..-." , "--." , "...." , ".." , ".---" , "-.-" , ".-.." , "--" , "-." , "---" , ".--." , "--.-" , ".-." , "..." , "-" , "..-" , "...-" , ".--" , "-..-" , "-.--" , "--.." ] lookup = {"".join(MORSE[ ord (c) - ord ( 'a' )] for c in word) \ for word in words} return len (lookup) |
Python: wo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class Solution( object ): def uniqueMorseRepresentations( self , words): """ :type words: List[str] :rtype: int """ m = [ ".-" , "-..." , "-.-." , "-.." , "." , "..-." , "--." , "...." , ".." , ".---" , "-.-" , ".-.." , "--" , "-." , "---" , ".--." , "--.-" , ".-." , "..." , "-" , "..-" , "...-" , ".--" , "-..-" , "-.--" , "--.." ] trans = [] res = 0 for word in words: temp = '' for c in word: temp + = m[ ord (c) - 97 ] if temp not in trans: trans.append(temp) res + = 1 return res |
C++:
1 2 3 4 5 6 7 8 9 10 | int uniqueMorseRepresentations(vector<string>& words) { vector<string> d = { ".-" , "-..." , "-.-." , "-.." , "." , "..-." , "--." , "...." , ".." , ".---" , "-.-" , ".-.." , "--" , "-." , "---" , ".--." , "--.-" , ".-." , "..." , "-" , "..-" , "...-" , ".--" , "-..-" , "-.--" , "--.." }; unordered_set<string> s; for ( auto word : words) { string code; for ( auto c : word) code += d[c - 'a' ]; s.insert(code); } return s.size(); } |
C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 | class Solution { public : int uniqueMorseRepresentations(vector<string>& words) { vector<string> morse{ ".-" , "-..." , "-.-." , "-.." , "." , "..-." , "--." , "...." , ".." , ".---" , "-.-" , ".-.." , "--" , "-." , "---" , ".--." , "--.-" , ".-." , "..." , "-" , "..-" , "...-" , ".--" , "-..-" , "-.--" , "--.." }; unordered_set<string> s; for (string word : words) { string t = "" ; for ( char c : word) t += morse[c - 'a' ]; s.insert(t); } return s.size(); } }; |
假定followup: 给一个单词的摩斯码,问有几种可能的单词,比如:"--...-.",至少有两种zen和gin
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构