반응형
Notice
Recent Posts
Recent Comments
- Today
- Total
작심삼일
[LeetCode] 804 | Unique Morse Code Words | Python 본문
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
반응형
'스터디 > 코테' 카테고리의 다른 글
[LeetCode] 326 | Power of Three | Python (0) | 2022.08.24 |
---|---|
[LeetCode] 1338 | Reduce Array size to The Half | Python (0) | 2022.08.18 |
[LeetCode] 387 | First Unique Character in a String | Python (0) | 2022.08.16 |
[LeetCode] 98 | Validate Binary Search Tree | Python (0) | 2022.08.11 |
[LeetCode] 108 | Convert Sorted Array to Binary Search Tree | Python (0) | 2022.08.10 |
Comments