Leetcode 2325: Decode the Message

grid47
grid47
Exploring patterns and algorithms
Mar 19, 2024 5 min read

Given a cipher key and a secret message, decode the message by replacing each letter with the corresponding letter in the alphabet based on the first appearance of each letter in the key. Spaces in the message remain unchanged.
Problem
Approach
Steps
Complexity
Input: The input consists of two strings: key, representing the cipher key, and message, representing the secret message to decode.
Example: key = "jumping over hills in the park", message = "swm tlnh dbe"
Constraints:
• The key will contain all 26 lowercase English letters at least once.
• The key may contain spaces, but they are ignored for decoding purposes.
• 1 <= message.length <= 2000
Output: Return the decoded message by applying the substitution table derived from the key.
Example: For key = "jumping over hills in the park" and message = "swm tlnh dbe", the output is "the quick fox".
Constraints:
• The decoded message should be returned as a string.
Goal: To decode the message using the substitution table derived from the key.
Steps:
• 1. Create a substitution table based on the first occurrence of each letter in the key.
• 2. Replace each letter in the message with the corresponding letter from the substitution table.
• 3. Return the decoded message, ensuring spaces remain unchanged.
Goal: The key string contains at least one occurrence of each letter from 'a' to 'z'. The message string consists of lowercase English letters and spaces.
Steps:
• 1 <= message.length <= 2000
• The key contains every letter from 'a' to 'z' at least once.
Assumptions:
• The key is a valid string of lowercase English letters containing all letters from 'a' to 'z'.
• The message is also valid, containing only lowercase English letters and spaces.
Input: key = "jumping over hills in the park", message = "swm tlnh dbe"
Explanation: Using the first occurrence of each letter in the key as a substitution table, we decode 'swm tlnh dbe' to 'the quick fox'.

Input: key = "smart minds solve problems", message = "jqjc bmcms wbs"
Explanation: Using the first occurrence of each letter in the key as a substitution table, we decode 'jqjc bmcms wbs' to 'the code works'.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus