Skip to content

Latest commit

 

History

History

1397

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

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, it is alphabetically greater than or equal to s1, it is alphabetically smaller than or equal to s2, and it does not contain the string evil as a substring. Since the answer can be a huge number, return this modulo 10^9 + 7.

 

Example 1:

Input: n = 2, s1 = "aa", s2 = "da", evil = "b"
Output: 51 
Explanation: There are 25 good strings starting with 'a': "aa","ac","ad",...,"az". Then there are 25 good strings starting with 'c': "ca","cc","cd",...,"cz" and finally there is one good string starting with 'd': "da". 

Example 2:

Input: n = 8, s1 = "leetcode", s2 = "leetgoes", evil = "leet"
Output: 0 
Explanation: All strings greater than or equal to s1 and smaller than or equal to s2 start with the prefix "leet", therefore, there is not any good string.

Example 3:

Input: n = 2, s1 = "gx", s2 = "gz", evil = "x"
Output: 2

 

Constraints:

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

Related Topics:
Dynamic Programming

Solution 1.

Let dp[i][j][k]

Let tr[i][j] be the maximum suffix length if we prepend character 'a' + j to a e's suffix of size i.

For example, if e = "abab", i = 2, j = 1, so the new suffix we get by prepending 'a' + 1 = 'b' to e[0..(i-1)] = "ab" is "bab", and "bab" is a suffix of e that is of size 3. So tr[2][1] = 3.

https://www.youtube.com/watch?v=uqdsHJzlsIc Digit DP: https://codeforces.com/blog/entry/53960