The Find All Good Strings coding problem is an advanced string-counting task. You are given two strings s1 and s2 of length , and a string evil. A "good" string is one that is lexicographically between s1 and s2 (inclusive) and does not contain evil as a substring. Your goal is to find the total count of such good strings, modulo .
This "Hard" difficulty problem is asked by top companies like Google to test a candidate's mastery of Digit Dynamic Programming and String Matching. It is significantly more complex than standard digit DP because the constraint is a pattern match rather than a simple numeric property. It evaluations your ability to combine DP state management with the KMP (Knuth-Morris-Pratt) algorithm to track the "matching state" of the evil string.
This problem uses Digit DP with a state that includes:
pos: Current character index being filled (0 to ).tight1: Boolean, whether the string is currently restricted by the prefix of s1.tight2: Boolean, whether the string is currently restricted by the prefix of s2.evil_match_len: The length of the longest prefix of evil that is currently a suffix of the string we are building.
You use the KMP state machine to update evil_match_len as you add each character. If evil_match_len ever equals the full length of evil, the current branch is invalid., s1="aa", s2="az", evil="ba"
s1 prefix (tight1=true) and is less than s2 (tight2=false).evil was "ab", then "ab" would be invalid.evil using indexOf or a naive search within the DP, which is inefficient.s1) and upper (s2) bounds simultaneously.This problem is essentially "Digit DP on Strings." Practice standard Digit DP first (like "Count numbers with sum of digits = K"). Once comfortable, learn how to integrate a finite state automaton (like KMP) into your DP transitions.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Maximum Repeating Substring | Easy | Solve | |
| String Transformation | Hard | Solve | |
| Count The Repetitions | Hard | Solve | |
| Encode String with Shortest Length | Hard | Solve | |
| Find the Occurrence of First Almost Equal Substring | Hard | Solve |