pyassistant

Count replacements avoiding palindromic substrings of length ≥2

Given a string s consisting of lowercase letters ('a'–'z') and the wildcard character '?', count the number of distinct strings you can obtain by replacing each '?' with any lowercase letter such that the final string contains no palindromic substring of length 2 or 3. Return the count modulo 1_000_000_007. A palindromic substring of length 2 is two equal adjacent characters ("aa"). A palindromic substring of length 3 is a substring of the form "axa" (first and third equal). You must avoid both types anywhere in the final string. Example constraints to consider: string length up to e.g. 200000 (your algorithm should be linear or near-linear in practice).

Example:

Input:
s = "a?b?"
Output:
650

Make sure you return your solution, don't print!

AI

Bot

Trying to solve my challenge? Ask if you must, or press the purple button so I can analyze your code.