Caesarian shift (Caesar cipher) is a method used in cryptography where a string
message is encrypted by shifting the letters
You are given an integer
n, which can be positive, negative or zero. Positive values indicate right shifts, and negative values indicate left shifts.
message encrypted by the shift
message = "abc"and
n = 3, the output should be
caesarian(message, n) = "def".
"a", shifted to the right
message = "egg"and
n = -1, the output should be
caesarian(message, n) = "dff".
- [time limit] 4000ms (js)
[input] string message
The message to be encrypted.
0 ≤ message.length ≤ 500.
[input] integer n
The shift value.
-2 · 109 ≤ n ≤ 2 · 109.
charCodeAt() and so on. A nice alternative is using the
Buffer object, which has a copy constructor for strings and can be casted to
Uint8Array like this:
new Uint8Array(new Buffer("hello"))
Buffer doesn't require the new-keyword and also the explicit call to
Uint8Array is not needed, since there is the spread-syntax in ES6, which minimizes the code to:
Now that we have an array of the ASCII values of the string, we can
map the values in order to shift them accoring to the given offset:
[...Buffer("hello")].map(x => f(x))
We now need to map the old ASCII-values to the new shifted values. In a perfectly aligned world, we would simply subtract 97 from the value in order to make 'a' zero, add the required shift and re-add the subtracted 97 after taking the modulo for the given congruence relation.
\[f(x) = (x - 97 + n) \bmod 26 + 97 \]
n % 26 gives us the interval \([-25, 25]\), which is okay because we add it to lowercase letters afterwards - which are always \(\geq 97\):
\[f(x) = (x + 7 + n\bmod 26) \bmod 26 + 97 \]
Bringing together these two pieces leads to the following solution:
b=Buffer caesarian = (m, n) => ""+b([...b(m)].map(x => (n % 26 + x + 7) % 26 + 97))
« Back to problem overview