2dbi

Caesar Cipher Decryption

viaLeetCode

Problem Decrypt an array/string of letters encoded with a Caesar-style shift: each plaintext letter was shifted forward by K (here framed as "decrypted letter is K−2 relative to the given one"); shift back with wraparound. Pin the exact shift semantics with an example before coding — the K−2 framing is deliberately confusing.

Input / Output

  • Input: encoded letters, shift parameter K. Output: decoded letters.

Constraints

  • Length and K up to 10^5 → reduce K mod 26 once; preserve case; leave non-letters untouched (clarify).

Example

  • With effective shift 2: "CD" → "AB"; wraparound "AB" with shift 2 → "YZ".

Expected approach

  • Per character: out = base + ((c − base − shift) mod 26 + 26) % 26, with base = 'A' or 'a' by case — the +26 guard fixes negative modulo (the hidden-test killer). O(n)/O(1). Edge cases to enumerate aloud: wraparound at 'A', mixed case, non-alphabetic passthrough, shift 0/26, large K. Mention encrypt/decrypt symmetry (±shift) and frequency analysis as the classical breaking follow-on.
Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account