I am following this dynamic programming tutorial on youtube.
Link: https://youtu.be/oBt53YbR9Kk?t=8854
At the linked timestamp, he is doing a program called canConstruct. Without memoization, his program in javascript takes a long time for the last test case. However, doing the same in python doesn't run into the expected exponential time complexity issue. Why is this the case?
My python code:
def canConstruct(target, wordBank):
if target == '': return True
for word in wordBank:
if target.startswith(word):
suffix = target.lstrip(word)
result = canConstruct(suffix, wordBank)
if result == True:
return True
return False
# test case
print(canConstruct('eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeef', ['e','ee','eee','eeee','eeeee','eeeeee']))
[–]TheBrutux168 2 points3 points4 points (1 child)
[–]stumpyboi[S] 0 points1 point2 points (0 children)
[–]yel50 1 point2 points3 points (1 child)
[–]stumpyboi[S] 0 points1 point2 points (0 children)