标签:matrix elf 判断 color orm and whether 字符串 return
"""
97. Interleaving String
Hard
Given s1, s2, 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
"""
现有三个字符串s1,s2,s3,判断s3是否是由s1,s2穿插拼接得到的,所谓穿插拼接就是说s1,s2看做是两个队列,里面的元素不断的进队,每次进队元素是哪个队伍是任意的,最后得到一个新的字符串,满足这样的条件就是穿插拼接.
可以用动态规划做,易得
class Solution(object): def isInterleave(self, s1, s2, s3): """ :type s1: str :type s2: str :type s3: str :rtype: bool """ l1, l2, l3 = len(s1), len(s2), len(s3) if l1 + l2 != l3: return False resultmatrix = [[None for j in range(l2+1)] for i in range(l1+1)] resultmatrix[0][0] = True for i in range(1, l1+1): resultmatrix[i][0] = resultmatrix[i-1][0] and (s1[i-1]==s3[i-1]) for j in range(1, l2+1): resultmatrix[0][j] = resultmatrix[0][j-1] and (s2[j-1]==s3[j-1]) for i in range(1, l1+1): for j in range(1, l2+1): e1, e2, e3 = s1[i-1], s2[j-1], s3[i+j-1] if e3 == e1 and e3 == e2: resultmatrix[i][j] = resultmatrix[i][j-1] or resultmatrix[i-1][j] elif e3 == e1: resultmatrix[i][j] = resultmatrix[i-1][j] elif e3 == e2: resultmatrix[i][j] = resultmatrix[i][j-1] else: resultmatrix[i][j] = False return resultmatrix[l1][l2]
标签:matrix elf 判断 color orm and whether 字符串 return
原文地址:https://www.cnblogs.com/mangmangbiluo/p/10358678.html