Skip to main content
  1. LeetCode/

LeetCode 2325: Decode the Message

·1 min· ·
LeetCode Hash Map Easy
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy
First Attempt: 2025-04-22

  • Total time: 10:00.00

Intuition
#

We can build a mapping from each character in the key to a corresponding character starting from 'a' by using ord().
After constructing the substitution table, we simply iterate through the message and join the result.
This is an O(n) solution that iterates through the array once.

Approach
#

class Solution:
    def decodeMessage(self, key: str, message: str) -> str:
        idx_map = {}
        first_ord = 97  # ASCII code for 'a'
        for s in key:
            if s != " " and s not in idx_map:
                idx_map[s] = chr(first_ord)
                first_ord += 1
        return "".join(idx_map[s] if s != " " else s for s in message)

Findings
#

N/A

Encountered Problems
#

N/A

Related

LeetCode 303: Range Sum Query - Immutable
·1 min
LeetCode Easy Prefix Sum
LeetCode Problem Solving
LeetCode 724: Find Pivot Index
·2 mins
LeetCode Daily Easy Prefix Sum
LeetCode Problem Solving
LeetCode 1534: Count Good Triplets
·3 mins
LeetCode Daily Easy Prefix Sum
LeetCode Problem Solving
LeetCode 3516: Find Closest Person
·1 min
LeetCode Weekly Easy
LeetCode Problem Solving
LeetCode 3512: Minimum Operations to Make Array Sum Divisible by K
·1 min
LeetCode Bi-Weekly Easy
LeetCode Problem Solving
LeetCode 2843: Count Symmetric Integers
·1 min
LeetCode Daily Easy Digit DP
LeetCode Problem Solving