Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0071e5a

Browse files
committedMay 14, 2025·
#2325: Decode the Message; solution & tests.
1 parent 828863f commit 0071e5a

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
 
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using NUnit.Framework;
2+
3+
using Problems;
4+
5+
public class DecodeTheMessageTests
6+
{
7+
[TestCase( "the quick brown fox jumps over the lazy dog", "vkbs bs t suepuv", ExpectedResult = "this is a secret" )]
8+
[TestCase( "eljuxhpwnyrdgtqkviszcfmabo", "zwx hnfx lqantp mnoeius ycgk vcnjrdb", ExpectedResult = "the five boxing wizards jump quickly" )]
9+
public string DecodeMessageTest( string key, string message ) =>
10+
new DecodeTheMessage().DecodeMessage( key, message );
11+
}

0 commit comments

Comments
 (0)
Please sign in to comment.