-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_interleaving.py
More file actions
80 lines (70 loc) · 1.95 KB
/
string_interleaving.py
File metadata and controls
80 lines (70 loc) · 1.95 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'''
page96
String C is said to be interleaving of string A and B if
it contains all the characters of A and B and the relative
order of characters of both the strings is preserved in C
'''
def WRONG_VERSION_solution(A, B, C):
'''
SEE THE PRINT VALUE
Input: A = 'xyz' B = 'abcd'
Output: C = xabyczd
'''
if len(C) != len(A) + len(B):
return False
i = 0
j = 0
k = 0
for i in range(len(C)):
print(C[i])
print(i,j,k)
if C[i] != A[j] and C[i] != B[k]:
return False
elif C[i] == A[j]:
j += 1
elif C[i] == B[k]:
k += 1
else:
return False
return True
def solution(A, B, C):
if len(A) == len(B) == len(C) == 0:
return True
if len(C) == 0:
return False
if len(A) == 0 and len(B) == 0:
return False
first = False
second = False
if A and A[0] == C[0]:
first = solution(A[1:],B, C[1:])
if B and B[0] == C[0]:
second = solution(A, B[1:], C[1:])
return first or second
def DP_solution(A,B,C):
memo = [[None for _ in range(len(B)+1)] for _ in range(len(A)+1)]
memo[0][0] = True
for i in range(1, len(A)+1):
if C[i-1] == A[i-1]:
memo[i][0] = memo[i-1][0]
else:
memo[i][0] = False
for j in range(1, len(B)+1):
if C[j-1] == B[j-1]:
memo[0][j] = memo[0][j-1]
else:
memo[0][j] = False
for i in range(1, len(A)+1):
for j in range(1, len(B)+1):
if C[i+j-1] != A[i-1] and \
C[i+j-1] != B[j-1]:
memo[i][j] = False
elif C[i+j-1] == A[i-1] and \
C[i+j-1] == B[j-1]:
memo[i][j] = memo[i-1][j] or memo[i][j-1]
elif C[i+j-1] == A[i-1]:
memo[i][j] = memo[i-1][j]
else:
memo[i][j] = memo[i][j-1]
print(memo)
return memo[len(A)][len(B)]