鱼C论坛

 找回密码
 立即注册
查看: 2517|回复: 0

[学习笔记] Leetcode 97. Interleaving String

[复制链接]
发表于 2020-9-24 22:36:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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



Example 1:

Screenshot from 2020-09-24 10-34-50.png

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.

  1. class Solution:
  2.     def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
  3.         m, n = len(s1), len(s2)
  4.         if m + n != len(s3): return False
  5.         
  6.         dp = [[False for _ in range(n + 1)] for _ in range(m + 1)]
  7.         
  8.         for i in range(m + 1):
  9.             for j in range(n + 1):
  10.                 if i == 0 and j == 0:
  11.                     dp[i][j] = True
  12.                     continue
  13.                
  14.                 dp[i][j] = False
  15.                
  16.                 if i > 0 and s3[i + j - 1] == s1[i - 1]:
  17.                     dp[i][j] |= dp[i - 1][j]
  18.                
  19.                 if j > 0 and s3[i + j - 1] == s2[j - 1]:
  20.                     dp[i][j] |= dp[i][j - 1]
  21.         
  22.         return dp[m][n]
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-4 19:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表