Problem · String

Batch Caesar Cipher

Learn this problem
EasyZoom logoZoomFULLTIMEONSITE INTERVIEW

Problem statement

Process a batch of Caesar-cipher operations. For index i, apply actions[i] to texts[i] using shifts[i].

  • encrypt moves each English letter forward through its alphabet.
  • decrypt moves each English letter backward through its alphabet.
  • Movement wraps independently within A through Z and a through z.
  • Every nonletter remains unchanged.

Return one transformed string for each operation, in input order. Reduce each shift modulo 26 before applying it.

Function

caesarCipher(actions: String[], texts: String[], shifts: int[]) → String[]

Examples

Example 1

actions = ["encrypt","decrypt","encrypt"]texts = ["Abc-XYZ!","Bcd-YZA!","Hello, World!"]shifts = [1,1,13]return = ["Bcd-YZA!","Abc-XYZ!","Uryyb, Jbeyq!"]

Uppercase and lowercase letters wrap within their own alphabets. Punctuation and spaces stay in place. A shift of 13 maps Hello, World! to Uryyb, Jbeyq!.

Example 2

actions = ["encrypt","decrypt"]texts = ["No change 123","Zz"]shifts = [0,52]return = ["No change 123","Zz"]

Shifts of 0 and 52 are both equivalent to no movement.

Constraints

  • 1 <= actions.length <= 10000
  • actions.length == texts.length == shifts.length
  • Each action is exactly encrypt or decrypt.
  • 0 <= texts[i].length <= 100000
  • The sum of all text lengths is at most 100000.
  • 0 <= shifts[i] <= 10^9
  • Every text contains printable ASCII characters.

More Zoom problems

drafts saved locally
public String[] caesarCipher(String[] actions, String[] texts, int[] shifts) {
    // write your code here
}
actions["encrypt","decrypt","encrypt"]
texts["Abc-XYZ!","Bcd-YZA!","Hello, World!"]
shifts[1,1,13]
expected["Bcd-YZA!", "Abc-XYZ!", "Uryyb", "Jbeyq!"]
checking account