2325. Decode the Message Easy

@problem@discussion
#Hash Table#String



1/**
2 * [2325] Decode the Message
3 *
4 * 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:
5 * <ol>
6 * Use the first appearance of all 26 lowercase English letters in key as the order of the substitution table.
7 * Align the substitution table with the regular English alphabet.
8 * Each letter in message is then substituted using the table.
9 * Spaces ' ' are transformed to themselves.
10 * </ol>
11 *
12 * For example, given key = "<u>hap</u>p<u>y</u> <u>bo</u>y" (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').
13 *
14 * Return the decoded message.
15 *  
16 * Example 1:
17 * <img alt="" src="https://assets.leetcode.com/uploads/2022/05/08/ex1new4.jpg" style="width: 752px; height: 150px;" />
18 * Input: key = "the quick brown fox jumps over the lazy dog", message = "vkbs bs t suepuv"
19 * Output: "this is a secret"
20 * Explanation: The diagram above shows the substitution table.
21 * It is obtained by taking the first appearance of each letter in "<u>the</u> <u>quick</u> <u>brown</u> <u>f</u>o<u>x</u> <u>j</u>u<u>mps</u> o<u>v</u>er the <u>lazy</u> <u>d</u>o<u>g</u>".
22 *
23 * Example 2:
24 * <img alt="" src="https://assets.leetcode.com/uploads/2022/05/08/ex2new.jpg" style="width: 754px; height: 150px;" />
25 * Input: key = "eljuxhpwnyrdgtqkviszcfmabo", message = "zwx hnfx lqantp mnoeius ycgk vcnjrdb"
26 * Output: "the five boxing wizards jump quickly"
27 * Explanation: The diagram above shows the substitution table.
28 * It is obtained by taking the first appearance of each letter in "<u>eljuxhpwnyrdgtqkviszcfmabo</u>".
29 *
30 * Constraints:
31 *
32 * 26 <= key.length <= 2000
33 * key consists of lowercase English letters and ' '.
34 * key contains every letter in the English alphabet ('a' to 'z') at least once.
35 * 1 <= message.length <= 2000
36 * message consists of lowercase English letters and ' '.
37 *
38 */
39pub struct Solution {}
40
41// problem: https://leetcode.com/problems/decode-the-message/
42// discuss: https://leetcode.com/problems/decode-the-message/discuss/?currentPage=1&orderBy=most_votes&query=
43
44// submission codes start here
45use std::collections::hash_map::Entry;
46use std::collections::HashMap;
47impl Solution {
48    pub fn decode_message(key: String, message: String) -> String {
49        let mut map = HashMap::new();
50        let mut vals = 'a'..='z';
51
52        for k in key.chars().filter(|&c| c != ' ') {
53            if let Entry::Vacant(e) = map.entry(k) {
54                e.insert(vals.next().unwrap());
55            }
56        }
57
58        message
59            .chars()
60            .map(|c| *map.get(&c).unwrap_or(&c))
61            .collect()
62    }
63}
64
65// submission codes end
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70
71    #[test]
72    fn test_2325() {
73        assert_eq!(
74            Solution::decode_message(
75                "the quick brown fox jumps over the lazy dog".to_string(),
76                "vkbs bs t suepuv".to_string()
77            ),
78            "this is a secret".to_owned()
79        );
80    }
81}
82


Back
© 2025 bowen.ge All Rights Reserved.