Count distinct passwords from one substring reversal

0
Sign in

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
  • password has 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.

AmazonCodingEasy

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.