34. Password Validation and Shift Cipher
Difficulty: medium · String, Simulation
Problem
Read a password and an integer `n`. **Validate** the password. It is valid only if all of these hold: 1. Its length is at least 8. 2. It contains at least one digit. 3. It contains at least one special character (any character that is not a letter or a digit). 4. It contains at least one uppercase letter. 5. It contains at least one lowercase letter. If any rule fails, print `Error!`. Otherwise **encrypt** it with a shift cipher: replace every character `c` with the character `n` positions after it in the printable ASCII range `!` (code 33) to `~` (code 126), wrapping around from `~` back to `!`. In other words the new code is `33 + (code(c) - 33 + n) mod 94`. Print the encrypted password. For example, `T@nuJ@in13` with `n = 2` becomes `VBpwLBkp35`.
Input
- Line 1: the password (printable ASCII characters with codes 33 to 126, no spaces). - Line 2: the integer `n`.
Output
Print the encrypted password, or `Error!` if it is invalid.
Example 1
Input: T@nuJ@in13 2 Output: VBpwLBkp35
Explanation: The password passes all five rules; each character moves 2 codes forward: T->V, @->B, n->p, u->w, J->L, i->k, 1->3, 3->5.
Example 2
Input: abc123!@ 5 Output: Error!
Explanation: There is no uppercase letter, so the password is invalid.
Example 3
Input:
Zz~9yY{}
3
Output:
]}#<|\~"Explanation: Valid. Shifting wraps past '~': Z->], z->}, ~->#, 9-><, y->|, Y->\, {->~, }->".
Constraints
- 1 <= length of password <= 1000 - 0 <= n <= 10^9
Solutions are judged against 3 sample and 12 hidden tests. Sign in to solve it · All problems