Find All Good Strings

Given the strings s1 and s2 of size n and the string evil, return the number of good strings.

A good string:

  • Has size n.
  • Is alphabetically greater than or equal to s1.
  • Is alphabetically smaller than or equal to s2.
  • Does not contain the string evil as a substring.

Since the answer can be a huge number, return it modulo 10^9 + 7.

Example 1
Inputn = 2, s1 = "aa", s2 = "da", evil = "b"
Output51
There are 25 good strings starting with 'a', 25 good strings starting with 'c', and one good string starting with 'd'.
Example 2
Inputn = 8, s1 = "leetcode", s2 = "leetgoes", evil = "leet"
Output0
All strings in the range start with the prefix "leet", so every such string contains evil and none are good.

Constraints

  • s1.length == n
  • s2.length == n
  • s1 <= s2
  • 1 <= n <= 500
  • 1 <= evil.length <= 50
  • All strings consist of lowercase English letters.

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate