68. Shift Cipher
A simple cipher moves every letter of the message s forward in the alphabet by k places, wrapping around from z back to a. Upper case letters stay upper case, lower case letters stay lower case, and every character that is not an English letter is left exactly as it is.
Return the encrypted message. Note that k can be much larger than 26.
Example 1
- Input:
- s = "Zebra-9", k = 3
- Output:
- "Cheud-9"
- Explanation:
Z wraps to C, e to h, b to e, r to u, a to d, while the dash and the digit stay: Cheud-9.
Example 2
- Input:
- s = "moon & stars", k = 13
- Output:
- "zbba & fgnef"
- Explanation:
Shifting by 13 turns moon into zbba and stars into fgnef, and the other characters stay: zbba & fgnef.
Constraints
1 ≤ s.length ≤ 105
0 ≤ k ≤ 109
s contains printable ASCII characters
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(n)
- Space
- O(n)