Problem solution · Python

Find All Good Strings

Find All Good Strings: a Python solution using dynamic programming. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Dynamic programming
Source
walkccc LeetCode Solutions
Length
58 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Dynamic programming

For Find All Good Strings, the implementation records answers for smaller states and reuses them to build the requested result without repeating work.

  1. Define precisely what one DP state represents.
  2. Establish the base cases before transitions are evaluated.
  3. Process states in dependency order and combine only already-known values.

Code notes

  • 58 lines of Python from the credited upstream file 1397.py.
  • The implementation visibly relies on sequence storage, cached states.
  • No explicit loop blocks detected.

Complexity

Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeFind All Good Strings · PythonPython
Use this to learn the idea, then write your own version.
class Solution:  def findGoodStrings(self, n: int, s1: str, s2: str, evil: str) -> int:    MOD = 1_000_000_007    evilLPS = self._getLPS(evil)     @functools.lru_cache(None)    def getNextMatchedEvilCount(j: int, currChar: str) -> int:      """      Returns the number of next matched evil count, where there're j matches      with `evil` and the current letter is ('a' + j).      """      while j > 0 and evil[j] != currChar:        j = evilLPS[j - 1]      return j + 1 if evil[j] == currChar else j     @functools.lru_cache(None)    def dp(i: int, matchedEvilCount: int, isS1Prefix: bool, isS2Prefix: bool) -> int:      """      Returns the number of good strings for s[i..n), where there're j matches      with `evil`, `isS1Prefix` indicates if the current letter is tightly bound      for `s1` and `isS2Prefix` indicates if the current letter is tightly bound      for `s2`.      """      # s[0..i) contains `evil`, so don't consider any ongoing strings.      if matchedEvilCount == len(evil):        return 0      # Run out of strings, so contribute one.      if i == n:        return 1      ans = 0      minCharIndex = ord(s1[i]) if isS1Prefix else ord('a')      maxCharIndex = ord(s2[i]) if isS2Prefix else ord('z')      for charIndex in range(minCharIndex, maxCharIndex + 1):        c = chr(charIndex)        nextMatchedEvilCount = getNextMatchedEvilCount(matchedEvilCount, c)        ans += dp(i + 1, nextMatchedEvilCount,                  isS1Prefix and c == s1[i],                  isS2Prefix and c == s2[i])        ans %= MOD      return ans     return dp(0, 0, True, True)   def _getLPS(self, pattern: str) -> list[int]:    """    Returns the lps array, where lps[i] is the length of the longest prefix of    pattern[0..i] which is also a suffix of this substring.    """    lps = [0] * len(pattern)    j = 0    for i in range(1, len(pattern)):      while j > 0 and pattern[j] != pattern[i]:        j = lps[j - 1]      if pattern[i] == pattern[j]:        lps[i] = j + 1        j += 1    return lps 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗