Decode the Message — LeetCode #2325 (Easy)
Tags: Hash Table, String
Related patterns: Hash Map, Array
Brute Force approach
Time complexity: O(n²). Space complexity: O(1).
The brute force approach involves manually mapping each character in the key to the corresponding character in the alphabet. This is straightforward but inefficient, as it requires checking each character multiple times.
The time complexity is O(n²) because for each character in the message, we may need to check against all characters in the key to find the mapping, leading to multiple iterations.
- Step 1: Create an empty mapping for characters from the key to the alphabet.
- Step 2: Iterate through each character in the key. For each character, if it hasn't been mapped yet, assign it the next available letter from the alphabet.
- Step 3: For each character in the message, replace it with the corresponding character from the mapping, leaving spaces unchanged.
1. Key: 'the quick brown fox jumps over the lazy dog', Message: 'vkbs bs t suepuv'
2. Mapping: { 't': 'a', 'h': 'b', 'e': 'c', 'q': 'd', 'u': 'e', 'i': 'f', 'k': 'g', 'b': 'h', 'r': 'i', 'o': 'j', 'w': 'k', 'n': 'l', 'f': 'm', 'x': 'n', 'j': 'o', 'm': 'p', 'p': 'q', 's': 'r', 'v': 's', 'l': 't', 'a': 'u', 'z': 'v', 'y': 'w', 'g': 'x', 'd': 'y' }
3. Decode 'v' -> 's', 'k' -> 'g', 'b' -> 'h', 's' -> 'r', ' ' -> ' ', 'b' -> 'h', 's' -> 'r', ' ' -> ' ', 't' -> 'a', ' ' -> ' ', 's' -> 'r', 'u' -> 'e', 'e' -> 'c', 'p' -> 'q', 'u' -> 'e', 'v' -> 's'. Final output: 'this is a secret'.
Optimal Solution approach
Time complexity: O(n). Space complexity: O(1).
The optimal solution efficiently constructs the mapping in a single pass through the key and then decodes the message in another pass, making it much faster than the brute force approach.
The time complexity is O(n) because we only make a single pass through the key and another pass through the message, making it efficient. The space complexity is O(1) since the mapping size is constant (26 letters).
- Step 1: Initialize an empty mapping and a variable to track the next available letter in the alphabet.
- Step 2: Iterate through the key, adding each unique character to the mapping until all 26 letters are mapped.
- Step 3: Create the decoded message by replacing each character in the message using the mapping.
1. Key: 'the quick brown fox jumps over the lazy dog', Message: 'vkbs bs t suepuv'
2. Mapping: { 't': 'a', 'h': 'b', 'e': 'c', 'q': 'd', 'u': 'e', 'i': 'f', 'k': 'g', 'b': 'h', 'r': 'i', 'o': 'j', 'w': 'k', 'n': 'l', 'f': 'm', 'x': 'n', 'j': 'o', 'm': 'p', 'p': 'q', 's': 'r', 'v': 's', 'l': 't', 'a': 'u', 'z': 'v', 'y': 'w', 'g': 'x', 'd': 'y' }
3. Decode 'v' -> 's', 'k' -> 'g', 'b' -> 'h', 's' -> 'r', ' ' -> ' ', 'b' -> 'h', 's' -> 'r', ' ' -> ' ', 't' -> 'a', ' ' -> ' ', 's' -> 'r', 'u' -> 'e', 'e' -> 'c', 'p' -> 'q', 'u' -> 'e', 'v' -> 's'. Final output: 'this is a secret'.
Key Insights
- The mapping is based on the first appearance of characters in the key.
- Spaces are preserved in the message and do not affect the mapping.
Common Mistakes
- Not checking if a character is already in the mapping, leading to incorrect substitutions.
- Forgetting to handle spaces correctly.
Interview Tips
- Clearly explain your thought process while coding.
- Test your solution with edge cases, such as keys with repeated characters.
- Be mindful of the space complexity, especially when dealing with large inputs.