Last active
August 29, 2015 14:01
-
-
Save zvonicek/a91561169a99edf244a3 to your computer and use it in GitHub Desktop.
isShuffle dynamic programming
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def isShuffle2(x,y,z): | |
m = len(x) | |
n = len(y) | |
r = len(z) | |
S = {} | |
#inicializace pole | |
for i in range(-1,r+1): | |
for j in range(-1,r+1): | |
S[(i,j)] = False | |
S[(0,0)] = True | |
for i in range(0,r+1): | |
for j in range(0,r+1): | |
if (i+j > r): | |
continue | |
if (i == 0 and j == 0): | |
S[(0,0)] = True | |
elif (x[(i-1) % m] == z[i+j-1] and y[(j-1) % n] != z[i+j-1]): | |
S[(i,j)] = S[(i-1,j)] | |
elif (x[(i-1) % m] != z[i+j-1] and y[(j-1) % n] == z[i+j-1]): | |
S[(i,j)] = S[(i,j-1)] | |
elif (x[(i-1) % m] == z[i+j-1] and y[(j-1) % n] == z[i+j-1]): | |
S[(i,j)] = S[(i,j-1)] or S[(i-1,j)] | |
for i in range(0,r+1): | |
for j in range(0,r+1): | |
if i+j == r and S[(i,j)]: | |
return True | |
return False | |
print isShuffle2("slovo", "word", "slwoovosrdlworovodwordwors") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment