Skip to content

Commit

Permalink
166th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Jul 15, 2024
1 parent 9864c17 commit 2665471
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/page-22/2325. Decode the Message/decodeMessage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { decodeMessage } from './decodeMessage';

describe('2325. Decode the Message', () => {
test('decodeMessage', () => {
{
const key = 'the quick brown fox jumps over the lazy dog';
const message = 'vkbs bs t suepuv';
const expected = 'this is a secret';
expect(decodeMessage(key, message)).toBe(expected);
}

{
const key = 'eljuxhpwnyrdgtqkviszcfmabo';
const message = 'zwx hnfx lqantp mnoeius ycgk vcnjrdb';
const expected = 'the five boxing wizards jump quickly';
expect(decodeMessage(key, message)).toBe(expected);
}
});
});
44 changes: 44 additions & 0 deletions src/page-22/2325. Decode the Message/decodeMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
type DecodeMessage = (key: string, message: string) => string;

/**
* Accepted
*/
export const decodeMessage: DecodeMessage = (key, message) => {
// Create a Map to store the substitution table
const substitutionMap = new Map<string, string>();

// Define the regular English alphabet
const alphabet = 'abcdefghijklmnopqrstuvwxyz';

// Variable to keep track of the current index in the alphabet
let substitutionIndex = 0;

// Loop through each character in the key string
for (const char of key) {
// Check if the character is a lowercase English letter and is not already in the map
if (char >= 'a' && char <= 'z' && !substitutionMap.has(char)) {
// Add the character to the map with its corresponding letter in the alphabet
substitutionMap.set(char, alphabet[substitutionIndex]);

// Move to the next letter in the alphabet
substitutionIndex += 1;
}
}

// Variable to store the decoded message
let decodedMessage = '';

// Loop through each character in the message string
for (const char of message) {
// If the character is a space, add a space to the decoded message
if (char === ' ') {
decodedMessage += ' ';
} else {
// Otherwise, substitute the character using the map and add it to the decoded message
decodedMessage += substitutionMap.get(char) || char;
}
}

// Return the decoded message
return decodedMessage;
};

0 comments on commit 2665471

Please sign in to comment.