2325. Decode the Message LeetCode Solution

In this guide, you will get 2325. Decode the Message LeetCode Solution with the best time and space complexity. The solution to Decode the Message problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Decode the Message solution in C++
  4. Decode the Message solution in Java
  5. Decode the Message solution in Python
  6. Additional Resources
2325. Decode the Message LeetCode Solution image

Problem Statement of 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”.

See also  1877. Minimize Maximum Pair Sum in Array LeetCode Solution

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 ' '.

Complexity Analysis

  • Time Complexity: O(|\texttt{key}| + |\texttt{message}|)
  • Space Complexity: O(128 + |\texttt{message}|) = O(|\texttt{message}|)

2325. Decode the Message LeetCode Solution in C++

class Solution {
 public:
  string decodeMessage(string key, string message) {
    vector<char> keyToActual(128);
    keyToActual[' '] = ' ';
    char currChar = 'a';

    for (const char c : key)
      keyToActual[c] = keyToActual[c] ?: currChar++;

    transform(message.cbegin(), message.cend(), message.begin(),
              [&](char c) { return keyToActual[c]; });

    return message;
  }
};
/* code provided by PROGIEZ */

2325. Decode the Message LeetCode Solution in Java

class Solution {
  public String decodeMessage(String key, String message) {
    StringBuilder sb = new StringBuilder();
    char[] keyToActual = new char[128];
    keyToActual[' '] = ' ';
    char currChar = 'a';

    for (final char c : key.toCharArray())
      if (keyToActual[c] == 0)
        keyToActual[c] = currChar++;

    for (final char c : message.toCharArray())
      sb.append(keyToActual[c]);

    return sb.toString();
  }
}
// code provided by PROGIEZ

2325. Decode the Message LeetCode Solution in Python

class Solution:
  def decodeMessage(self, key: str, message: str) -> str:
    keyToActual = {' ': ' '}
    currChar = 'a'

    for c in key:
      if c not in keyToActual:
        keyToActual[c] = currChar
        currChar = chr(ord(currChar) + 1)

    return ''.join(keyToActual[c] for c in message)
# code by PROGIEZ

Additional Resources

See also  664. Strange Printer LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.