804. 唯一摩尔斯密码词

exiaohu 于 2022-04-10 发布

题目链接:804. 唯一摩尔斯密码词

简单模拟。

from typing import List


class Solution:
    def uniqueMorseRepresentations(self, words: List[str]) -> int:
        def lookup(char: str) -> str:
            return [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
                    ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
                    "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."][ord(char) - ord('a')]

        def translate(word: str) -> str:
            return ''.join(lookup(char) for char in word)

        ans = set()
        for word in words:
            ans.add(translate(word))

        return len(ans)