Skip to main content
  1. LeetCode/

LeetCode 2325: Decode the Message

·1 min· ·
LeetCode Daily Easy Hash-Map
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 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 2843: Count Symmetric Integers
·1 min
LeetCode Daily Easy Digit-Dp
LeetCode Problem Solving
LeetCode 617: Merge Two Binary Trees
·2 mins
LeetCode Daily Easy Tree
LeetCode Problem Solving
LeetCode 2145: Count the Hidden Sequences
·2 mins
LeetCode Daily Medium
LeetCode Problem Solving
LeetCode 2799: Count Complete Subarrays in an Array
·2 mins
LeetCode Daily Medium Sliding-Window
LeetCode Problem Solving