← Questions

Count distinct passwords from one substring reversal

0
AmazonCoding

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.

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding