|
| 1 | +/** |
| 2 | + * 2325 |
| 3 | + * Decode the Message |
| 4 | + ** |
| 5 | + * You are given the strings key and message, |
| 6 | + * which represent a cipher key and a secret message, respectively. |
| 7 | + * |
| 8 | + * The steps to decode message are as follows: |
| 9 | + * 1. Use the first appearance of all 26 lowercase English letters |
| 10 | + * in key as the order of the substitution table. |
| 11 | + * 2. Align the substitution table with the regular English alphabet. |
| 12 | + * 3. Each letter in message is then substituted using the table. |
| 13 | + * 4. Spaces ' ' are transformed to themselves. |
| 14 | + * |
| 15 | + * For example, |
| 16 | + * given key = "happy boy" |
| 17 | + * (actual key would have at least one instance of each letter in the alphabet), |
| 18 | + * we have the partial substitution table of |
| 19 | + * ('h' -> 'a', 'a' -> 'b', 'p' -> 'c', 'y' -> 'd', 'b' -> 'e', 'o' -> 'f'). |
| 20 | + * Return the decoded message. |
| 21 | + * |
| 22 | + * Example 1: |
| 23 | + * Input: key = "the quick brown fox jumps over the lazy dog", message = "vkbs bs t suepuv" |
| 24 | + * Output: "this is a secret" |
| 25 | + * Explanation: |
| 26 | + * The diagram above shows the substitution table. |
| 27 | + * It is obtained by taking the first appearance of each letter |
| 28 | + * in "the quick brown fox jumps over the lazy dog". |
| 29 | + * |
| 30 | + * Example 2: |
| 31 | + * Input: key = "eljuxhpwnyrdgtqkviszcfmabo", message = "zwx hnfx lqantp mnoeius ycgk vcnjrdb" |
| 32 | + * Output: "the five boxing wizards jump quickly" |
| 33 | + * Explanation: |
| 34 | + * The diagram above shows the substitution table. |
| 35 | + * It is obtained by taking the first appearance of each letter in "eljuxhpwnyrdgtqkviszcfmabo". |
| 36 | + * |
| 37 | + * Constraints: |
| 38 | + * • 26 <= key.length <= 2000 |
| 39 | + * • key consists of lowercase English letters and ' '. |
| 40 | + * • key contains every letter in the English alphabet ('a' to 'z') at least once. |
| 41 | + * • 1 <= message.length <= 2000 |
| 42 | + * • message consists of lowercase English letters and ' '. |
| 43 | + * |
| 44 | + * Hint 1: |
| 45 | + * Iterate through the characters in the key to construct a mapping to the English alphabet. |
| 46 | + * |
| 47 | + * Hint 2: |
| 48 | + * Make sure to check that the current character is not already in the mapping (only the first appearance is considered). |
| 49 | + * |
| 50 | + * Hint 3: |
| 51 | + * Map the characters in the message according to the constructed mapping. |
| 52 | + ** |
| 53 | + * https://leetcode.com/problems/decode-the-message/ |
| 54 | +***/ |
| 55 | + |
| 56 | +using System; |
| 57 | + |
| 58 | +namespace Problems; |
| 59 | + |
| 60 | +public class DecodeTheMessage |
| 61 | +{ |
| 62 | + public string DecodeMessage( string key, string message ) |
| 63 | + { |
| 64 | + char[] map = new char[123]; |
| 65 | + map[' '] = ' '; |
| 66 | + |
| 67 | + char mapIndex = 'a'; |
| 68 | + |
| 69 | + foreach ( char c in key ) |
| 70 | + { |
| 71 | + if ( map[c] == 0 ) |
| 72 | + { |
| 73 | + map[c] = mapIndex++; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + char[] result = new char[message.Length]; |
| 78 | + |
| 79 | + for ( int i = 0; i < message.Length; i++ ) |
| 80 | + { |
| 81 | + result[i] = map[message[i]]; |
| 82 | + } |
| 83 | + |
| 84 | + return new String( result ); |
| 85 | + } |
| 86 | +} |
0 commit comments