작심삼일

[LeetCode] 804 | Unique Morse Code Words | Python 본문

스터디/코테

[LeetCode] 804 | Unique Morse Code Words | Python

yun_s 2022. 8. 17. 09:06
728x90
반응형

문제 링크: https://leetcode.com/problems/unique-morse-code-words/


문제

International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows:

  • 'a' maps to ".-",
  • 'b' maps to "-...",
  • 'c' maps to "-.-.", and so on.

For convenience, the full table for the 26 letters of the English alphabet is given below:

[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.",
"---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]

Given an array of strings words where each word can be written as a concatenation of the Morse code of each letter.

  • For example, "cab" can be written as "-.-..--...", which is the concatenation of "-.-.", ".-", and "-...". We will call such a concatenation the transformation of a word.

Return the number of different transformations among all words we have.


조건

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 12
  • words[i] consists of lowercase English letters.

내 풀이

모든 단어로 모스부호를 만든뒤에 겹치지 않는 것만 ans에 저장한다.

ans의 길이를 출력하면 된다.


코드

class Solution:
    def uniqueMorseRepresentations(self, words: List[str]) -> int:
        morse = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.",
                 "--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
        
        ans = []
        for word in words:
            trans = ''
            for char in word:
                trans += morse[ord(char)-ord('a')]
            
            if trans not in ans:
                ans.append(trans)
            
        return len(ans)
728x90
반응형
Comments