首页 > 编程知识 正文

Leetcode-804福尔摩斯密码

时间:2023-05-06 08:40:59 阅读:239021 作者:1217

题目描述:

    

国际lhzdzxc密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: "a" 对应 ".-", "b" 对应 "-...", "c" 对应 "-.-.", 等等。

为了方便,所有26个英文字母对应lhzdzxc密码表如下:

[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]

给定一个单词列表,每个单词可以写成每个字母对应lhzdzxc密码的组合。例如,"cab" 可以写成 "-.-.-....-",(即 "-.-." + "-..." + ".-"字符串的结合)。我们将这样一个连接过程称作单词翻译。

返回我们可以获得所有词不同单词翻译的数量。

例如:输入: words = ["gin", "zen", "gig", "msg"]输出: 2解释: 各单词翻译如下:"gin" -> "--...-.""zen" -> "--...-.""gig" -> "--...--.""msg" -> "--...--."共有 2 种不同翻译, "--...-." 和 "--...--."

注意:

单词列表words 的长度不会超过 100。每个单词 words[i]的长度范围为 [1, 12]。每个单词 words[i]只包含小写字母。

代码:

class Solution(object): def uniqueMorseRepresentations(self, words): """ :type words: List[str] :rtype: int """ list0=[] out = 0 listABC = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'] listmorse = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] for str1 in words: str2 = '' for i in range(0,len(str1)): str2 = str2+listmorse[listABC.index(str1[i])] if(str2 in list0): continue else: list0.append(str2) out +=1 return out

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。