HackerRank Caesar's Cipher solution with JavaScript

Learn how to solve the Caesar's Cipher problem from the HackerRank challenges with JavaScript.
May 06, 2023JavaScript

The HackerRank Caesar's Cipher problem tells us that Julius Caesar encrypted his confidential information using a cipher. Caesar's cipher shifts each letter by a number of letters. The cipher only encrypts letters, not symbols.

Example

Original alphabet

``````abcdefghijklmnopqrstuvwxyz;
``````

Alphabet rotated by 3 letters

``````defghijklmnopqrstuvwxyzabc;
``````

If the shifting of the letters goes past the end of the alphabet, we are told to just rotate back to the front of the alphabet. In the case of a rotation by 3, w, x, y and z would map to z, a, b and c.

We are asked to define a `caesarCipher` function that receives a string of characters and an integer that represents the alphabet rotation factor. This function should return the encrypted string.

Pseudo code solution

1. Define a string containing all lowercase alphabet characters.
2. Define a string containing all uppercase alphabet characters.
3. Map over the characters in the input string.
• If the current character is in the string of lowercase characters, find its position in the string and add 3 to it. Use the remainder operator to ensure that this addition does not exceed the 26 positions in the string.
• If the current character is in the string of uppercase characters, find its position in the string and add 3 to it. Use the remainder operator to ensure that this addition does not exceed the 26 positions in the string.
• If it's not an alphabetical character, return it as-is.
4. Return a string representation of the array of characters returned by mapping over the input string.

Code solution

``````function caesarCipher(s, k) {
const lowerA = 'abcdefghijklmnopqrstuvwxyz';
const upperA = lowerA.toUpperCase();

const res = s.split('').map((c) => {
if (lowerA.includes(c)) {
return lowerA[(lowerA.indexOf(c) + k) % 26];
} else if (upperA.includes(c)) {
return upperA[(upperA.indexOf(c) + k) % 26];
} else {
// any other characters
return c;
}
});

return res.join('');
}
``````

If `indexOf(c)` is `25` and `k` is `2`, adding them gives us `27`, which exceeds the 26-character alphabet. Therefore, we applied the remainder operator to clamp the addition operation to 26 (`27 % 26 = 1`).

Test cases

Let's test the `caesarCipher` function above with the following test case.

``````console.log(caesarCipher('middle-Outz', 2)); // okffng-Qwvb;
``````

The result is what we expect. The special hyphen character in the string is left as-is. The capital `O` in the string becomes `Q` after adding a `k` of `2` to it. Lastly, manually moving each lowercase character ahead by a `k` of `2` helps us confirm that the cipher is correct.

Time and space complexity

The Big O time complexity of the solution is linear, or `O(n)`, where `n` is the length of the input string. The solution used one loop that iterated over all the characters in the input string.

The Big O space complexity of the solution is linear, or `O(26 * 2)`. The length of the `lowerA` string and the `upperA` string that we defined for our solution carries a space complexity of 2 times the length of the alphabet.