Count distinct passwords from one substring reversal
I was asked this
No reports yet. Sign in to say where you were asked it.
A security team scores a password by its variability. Variability is the number of distinct strings you can get by reversing exactly one contiguous substring of the password.
Reversing a single character, or any palindromic substring, leaves the password unchanged. So the original password always counts once.
Implement:
def count_distinct_passwords(password: str) -> int
Return the variability of password.
Constraints
1 <= len(password) <= 10^5-
passwordhas only lowercase English letters. - The answer can be larger than
2^31 - 1.
Example 1
Input: password = "xyz"
Output: 4
The reachable strings are "xyz" (unchanged), "yxz", "xzy", and "zyx".
Example 2
Input: password = "ppq"
Output: 3
The reachable strings are "ppq", "pqp", and "qpp". Reversing "pp" gives back the original.
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.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
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.
Sign in to submit your code; every attempt is kept here.
