Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 파이썬 프로그래머스
- 잇츠디모
- python Leetcode
- python xor
- leetcode 풀기
- 릿코드 파이썬
- 릿코드풀기
- 코틀린기초
- 상가수익률계산기
- LeetCode
- 파이썬알고리즘
- 파이썬알고리즘풀기
- 파이썬릿코드
- python 릿코드
- 알고리즘풀기
- python sorted
- python priority queue
- 파이썬 릿코드
- leetcode풀이
- 릿코드 풀기
- leetcode풀기
- python zip_longest
- 파이썬릿코드풀기
- binary search
- 파이썬 알고리즘 풀기
- 파이썬 알고리즘
- python 알고리즘
- 릿코드풀이
- 알고리즘풀이
- 릿코드
Archives
- Today
- Total
소프트웨어에 대한 모든 것
2325. Decode the Message 본문
반응형
제목
문제)
You are given the strings key and message, which represent a cipher key and a secret message, respectively. The steps to decode message are as follows:
- Use the first appearance of all 26 lowercase English letters in key as the order of the substitution table.
- Align the substitution table with the regular English alphabet.
- Each letter in message is then substituted using the table.
- Spaces ' ' are transformed to themselves.
- For example, given key = "happy boy" (actual key would have at least one instance of each letter in the alphabet), we have the partial substitution table of ('h' -> 'a', 'a' -> 'b', 'p' -> 'c', 'y' -> 'd', 'b' -> 'e', 'o' -> 'f').
Return the decoded message.
Example 1:
Input: key = "the quick brown fox jumps over the lazy dog", message = "vkbs bs t suepuv"
Output: "this is a secret"
Explanation: The diagram above shows the substitution table.
It is obtained by taking the first appearance of each letter in "the quick brown fox jumps over the lazy dog".
Example 2:
Input: key = "eljuxhpwnyrdgtqkviszcfmabo", message = "zwx hnfx lqantp mnoeius ycgk vcnjrdb"
Output: "the five boxing wizards jump quickly"
Explanation: The diagram above shows the substitution table.
It is obtained by taking the first appearance of each letter in "eljuxhpwnyrdgtqkviszcfmabo".
Constraints:
- 26 <= key.length <= 2000
- key consists of lowercase English letters and ' '.
- key contains every letter in the English alphabet ('a' to 'z') at least once.
- 1 <= message.length <= 2000
- message consists of lowercase English letters and ' '.
솔루션1)
- hasing을 사용해서 문제 해결
class Solution:
def decodeMessage(self, key: str, message: str) -> str:
alphabets = deque(string.ascii_lowercase)
encoder = {}
for c in key:
# skip space
if c == ' ':
continue
if c not in encoder:
encoder[c] = alphabets.popleft()
if len(alphabets) == 0:
break
encoded_message = []
for c in message:
if c == ' ':
encoded_message.append(' ')
else:
encoded_message.append(encoder[c])
return ''.join(encoded_message)
반응형
'알고리즘 > LeetCode' 카테고리의 다른 글
2352. Equal Row and Column Pairs (0) | 2022.12.23 |
---|---|
2373. Largest Local Values in a Matrix (0) | 2022.12.23 |
1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree (2) | 2022.12.23 |
2265. Count Nodes Equal to Average of Subtree (0) | 2022.12.23 |
2433. Find The Original Array of Prefix Xor (0) | 2022.12.23 |
Comments