本题用到Python基本数据类型set,set是一个无序且不重复的元素集合。
class Solution(object):
def uniqueMorseRepresentations(self, words):
"""
:type words: List[str]
:rtype: int
"""
Morse = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.",
"---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
res = set()
for i in range(len(words)):
str=""
for j in range(len(words[i])):
str = str + Morse[ord(words[i][j])-ord('a')]
res.add(str)
return len(res)