Find All Good Strings - Problem

You're tasked with finding "good strings" that satisfy multiple strict criteria. Given three strings - s1, s2 (both of length n), and evil - you need to count all strings of length n that are:

  • Lexicographically โ‰ฅ s1 (comes at or after s1 in dictionary order)
  • Lexicographically โ‰ค s2 (comes at or before s2 in dictionary order)
  • Does NOT contain evil as a substring

For example, if s1 = "aa", s2 = "da", and evil = "b", then valid strings include "aa", "ac", "ca", etc., but NOT "ab" (contains evil) or "ea" (> s2).

Return the count modulo 109 + 7 since the answer can be enormous.

Input & Output

example_1.py โ€” Basic Case
$ Input: n = 2, s1 = "aa", s2 = "da", evil = "b"
โ€บ Output: 51
๐Ÿ’ก Note: All strings from 'aa' to 'da' that don't contain 'b'. This includes 'aa', 'ac', 'ad', ..., 'ca', 'cc', 'cd', ..., 'da' but excludes any string with 'b' like 'ab', 'ba', 'bb', etc.
example_2.py โ€” Evil at Start
$ Input: n = 8, s1 = "leetcode", s2 = "leetgoes", evil = "leet"
โ€บ Output: 0
๐Ÿ’ก Note: Since both s1 and s2 start with 'leet' (which is the evil string), any valid string in the range must also start with 'leet', making it impossible to avoid the evil substring.
example_3.py โ€” Single Character Range
$ Input: n = 2, s1 = "gx", s2 = "gz", evil = "x"
โ€บ Output: 2
๐Ÿ’ก Note: Valid strings are 'gy' and 'gz'. 'gx' is excluded because it contains the evil character 'x'.

Visualization

Tap to expand
Smart String Construction ProcessPosition 0Choose: s1[0] to s2[0]KMP State: 0Position 1Update boundsUpdate KMPPosition ...Continue buildingAvoid evil patternKMP Pattern TrackerState 0: No matchState 1: Partial matchState EVIL: REJECT!
Understanding the Visualization
1
Setup KMP Automaton
Create a state machine that tracks how close we are to forming the forbidden pattern
2
Dynamic Programming
Build strings position by position, maintaining bounds and pattern state
3
Pruning
Skip any path that would complete the forbidden pattern
4
Count Valid Strings
Sum up all successful paths that reach the end without violations
Key Takeaway
๐ŸŽฏ Key Insight: By combining digit DP for lexicographic constraints with KMP automaton for pattern avoidance, we can efficiently explore only valid string constructions without generating invalid paths.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n ร— m ร— 8)

n positions, m KMP states, 8 combinations of tight bounds, constant work per state

n
2n
โœ“ Linear Growth
Space Complexity
O(n ร— m ร— 8)

Memoization table with dimensions for position, KMP state, and tight flags

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค n โ‰ค 500
  • 1 โ‰ค evil.length โ‰ค 50
  • s1.length == s2.length == n
  • s1 โ‰ค s2 lexicographically
  • s1, s2, and evil consist of lowercase English letters only
Asked in
Google 15 Amazon 8 Microsoft 5 Meta 3
28.0K Views
Medium Frequency
~35 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen