python leetcode练习(804.独特的摩尔斯电码)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python leetcode练习(804.独特的摩尔斯电码)相关的知识,希望对你有一定的参考价值。

class Solution:
    def uniqueMorseRepresentations(self, words):
        """
        :type words: List[str]
        :rtype: int
        """
        # creat two lists for mapping later      
        aphabet_list = ['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_list = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]        
        list_trans = []
        # loop through words and transform the words
        for word in words:
            trans = ''
            for c in word:                
                trans += morse_list[aphabet_list.index(c)]
            list_trans.append(trans)
        list_remove_repeat = set(list_trans)  # use set() to remove the duplicate element
        return len(list_remove_repeat)

以上是关于python leetcode练习(804.独特的摩尔斯电码)的主要内容,如果未能解决你的问题,请参考以下文章

leecode练习--804唯一摩尔斯密码词

LeetCode 804 唯一摩尔斯密码词

leetcode804

leetcode804

LeetCode 804. Unique Morse Code Words

LeetCode804. 唯一摩尔斯密码词