forked from y-ncao/Python-Study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterleaving_String.py
59 lines (51 loc) · 1.87 KB
/
Interleaving_String.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.
For example,
Given:
s1 = "aabcc",
s2 = "dbbca",
When s3 = "aadbbcbcac", return true.
When s3 = "aadbbbaccc", return false.
"""
class Solution:
# @return a boolean
def isInterleave(self, s1, s2, s3):
return self.isInterleave_1(s1, s2, s3)
def isInterleave_1(self, s1, s2, s3):
M = len(s1)
N = len(s2)
K = len(s3)
if M + N != K:
return False
dp = [ [ False for j in range(N+1)] for i in range(M+1) ]
for i in range(M+1):
for j in range(N+1):
if i == 0 and j == 0:
dp[i][j] = True
elif i > 0 and dp[i-1][j] and s1[i-1] == s3[i-1+j]:
dp[i][j] = True
elif j > 0 and dp[i][j-1] and s2[j-1] == s3[i+j-1]:
dp[i][j] = True
else:
dp[i][j] = False
return dp[M][N]
# Note:
# 1. dp[i][j] means whether s1[:i] and s2[:j] is interleave with s3[:i+j]
# 2. dp[0...M][0...N] = False
# 3. dp[i][j] = True # if dp[i-1][j] == True and s1[i-1] == s3[i-1+j] or
# dp[i][j-1] == True and s2[j-1] == s3[i+j-1]
# = False # else
# 4. dp[M][N]
# Will TLE
def isInterleave_2(self, s1, s2, s3):
return self.isInterleave_re(s1, 0, s2, 0, s3, 0)
def isInterleave_re(self, s1, i1, s2, i2, s3, i3):
if i1 >= len(s1) and i2 >= len(s2) and i3 >= len(s3):
return True
if i3 >= len(s3):
return False
if i1 >= len(s1):
return s2[i2:] == s3[i3:]
if i2 >= len(s2):
return s1[i1:] == s3[i3:]
return (s1[i1] == s3[i3] and self.isInterleave_re(s1, i1+1, s2, i2, s3, i3+1)) or (s2[i2] == s3[i3] and self.isInterleave_re(s1, i1, s2, i2+1, s3, i3+1))