方法一:模拟法
1.将字符串列表转换成摩尔斯字符串列表
2.再将摩尔斯字符串列表转换成集合
3.求集合的长度即为不同摩尔斯密码的个数(利用集合中的元素不能重复特性)
class Solution:
def uniqueMorseRepresentations(self, words: List[str]) -> int:
password ={'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':"--.."}
l = len(words)
my_list = [""] * l
for i in range(l):
n = len(words[i])
for j in range(n):
my_list[i] += password[words[i][j]]
se = set(my_list) #将列表转换成集合
l2 = len(se)
return l2