Password similarity under cyclic character bumps
A security team wants to reject a new password when it is too close to the user's previous one.
You may transform the new password like this: pick any set of its positions (possibly none) and replace each chosen character with the next letter of the alphabet, cyclically (a becomes b, m becomes n, z becomes a). Each position is bumped at most once.
The two passwords are similar if, for some choice of positions, the old password is a subsequence of the transformed new password. A subsequence keeps the original order but may skip characters.
You are given two lists of the same length, new_passwords and old_passwords. For each index i, decide whether new_passwords[i] and old_passwords[i] are similar.
Implement check_similarities(new_passwords: list[str], old_passwords: list[str]) -> list[str], returning "YES" or "NO" for each pair.
Constraints
1 <= len(new_passwords) == len(old_passwords) <= 101 <= len(new_passwords[i]), len(old_passwords[i]) <= 2 * 10^5- All passwords contain only lowercase English letters.
Example 1
new_passwords = ["abdbc"], old_passwords = ["bcd"]
Output: ["YES"]
Bump the first a to b and the following b to c, giving bcdbc. The first three characters spell bcd.
Example 2
new_passwords = ["aaa", "zb"], old_passwords = ["c", "ab"]
Output: ["NO", "YES"]
An a can only become a or b, so c never appears. In the second pair, z wraps around to a and b stays, giving ab.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
