—Easy
https://leetcode.com/problems/unique-morse-code-words/
Code:
class Solution: def uniqueMorseRepresentations(self, words) -> int: dict_umcw = {'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':"--.."} res_tran = [] ans_cont = 0 for word in words: tmp_trans = "" for elt in word: tmp_trans += dict_umcw[elt] if tmp_trans not in res_tran: res_tran.append(tmp_trans) ans_cont += 1 return ans_cont # s = Solution() # print(s.uniqueMorseRepresentations(words = ["gin", "zen", "gig", "msg"]))
思路:
1.遍历,以后练习set操作