leetcode - 804 - 唯一摩尔斯密码词

class Solution:   
    def uniqueMorseRepresentations(self, words):
        """
        :type words: List[str]
        :rtype: int
        """
        dic = {"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":"--.."}
        morse=''
        morse_list = []
        mor_list = []
        diff_morse=0
        for word in words:
            word = word.lower()
            for i in word:
                morse += dic[i]
            morse_list.append(morse)
            morse = ''
     
        for mor in morse_list:
            if mor not in mor_list:
                mor_list.append(mor)
                diff_morse += 1
        return diff_morse
    

猜你喜欢

转载自blog.csdn.net/hustwayne/article/details/83545094