Solving Leetcode Interviews in Seconds with AI: Decode the Message
Introduction
In this blog post, we will explore how to solve the LeetCode problem "2325" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
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 ' '.
Explanation
Here's the breakdown of the approach, complexities, and the Python code:
High-Level Approach:
- Create a mapping (dictionary) from the unique characters in the
keyto the alphabet. - Iterate through the
message, substituting each character using the created map. - Handle spaces by keeping them as is.
- Create a mapping (dictionary) from the unique characters in the
Complexity:
- Runtime Complexity: O(m + k), where m is the length of the message and k is the length of the key.
- Storage Complexity: O(1), the size of the alphabet is constant.
Code
def decodeMessage(key: str, message: str) -> str:
"""
Decodes a message using a substitution cipher based on the given key.
Args:
key: The cipher key string.
message: The message string to decode.
Returns:
The decoded message string.
"""
mapping = {}
alphabet = "abcdefghijklmnopqrstuvwxyz"
key_index = 0
alphabet_index = 0
for char in key:
if char != ' ' and char not in mapping:
mapping[char] = alphabet[alphabet_index]
alphabet_index += 1
decoded_message = ""
for char in message:
if char == ' ':
decoded_message += ' '
else:
decoded_message += mapping[char]
return decoded_message