Skip to content

Latest commit

 

History

History
50 lines (45 loc) · 2.77 KB

풀이_LC804.md

File metadata and controls

50 lines (45 loc) · 2.77 KB

🩳 804. Unique Morse Code Words

  • Date : 2021.12.05(일)
  • Time : 20분

Problem

  • 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.

Constraints

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

Example

  • Input: words = ["gin","zen","gig","msg"]
  • Output: 2
  • Explanation: The transformation of each word is:
    "gin" -> "--...-."
    "zen" -> "--...-."
    "gig" -> "--...--."
    "msg" -> "--...--."
    There are 2 different transformations: "--...-." and "--...--.".
    



풀이

    def uniqueMorseRepresentations(self, words: List[str]) -> int:
        mos = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
        answerArray = set()
        for i in words:
            chageMos = []
            for j in i:
                chageMos+=[mos[ord(j)-ord('a')]]
            answerArray.add("".join(chageMos))
        return len(answerArray)

: 이 문제는 문자들은 특정한 모스부호로 변경하는 문제이다. 그래서 for 문을 통해 배열에 담긴 문자를 하나씩 불러와주었다. 그리고 그 문자에서도 문자열을 하나씩 불러주었다. 그리고 mos에 담긴 문자로 하나씩 매핑을 해줬어야 했는데 이걸 조금 고민했다. 고민하다가 ord()함수를 이용해 유니코드 값으로 변경해주었다. 그리고 mos 배열에서는 'a'가 0 인덱스를 가지기 때문에 a의 유니코드 값을 빼주어서 인덱스 값을 구할 수 있었다. 아니면 튜플을 이용해서 애초에 알파벳과 매핑해주는 방법도 있을 것 같다. 그리고 문자열을 answerArray에 넣어주었다. 그리고 이 문제 특성상 다른 문자여도 변환값이 같게 나올 수 있다. 근데 중복값을 걸러주라는 조건이 있었기 때문에 애초에 중복값은 저장되지 않는 Set()을 사용했다.