Seawolf 发表于 2020-9-24 22:36:01

Leetcode 97. Interleaving String

Given s1, s2, and s3, find whether s3 is formed by the interleaving of s1 and s2.



Example 1:



Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
Output: true
Example 2:

Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
Output: false
Example 3:

Input: s1 = "", s2 = "", s3 = ""
Output: true


Constraints:

0 <= s1.length, s2.length <= 100
0 <= s3.length <= 200
s1, s2, and s3 consist of lower-case English letters.

class Solution:
    def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
      m, n = len(s1), len(s2)
      if m + n != len(s3): return False
      
      dp = [ for _ in range(m + 1)]
      
      for i in range(m + 1):
            for j in range(n + 1):
                if i == 0 and j == 0:
                  dp = True
                  continue
               
                dp = False
               
                if i > 0 and s3 == s1:
                  dp |= dp
               
                if j > 0 and s3 == s2:
                  dp |= dp
      
      return dp
页: [1]
查看完整版本: Leetcode 97. Interleaving String