Please provide me a step by step and easy explanation as to why the following code is the solution to the prompt. Please be specific and showing examples would be much appreciated. Also, please be mindful of your work, since I have experience of receiving answers that seems like the "expert" didn't even read the question. Thank you.
Write a function, quickest_concat, that takes in a string and a list of words as arguments. The function should return the minimum number of words needed to build the string by concatenating words of the list.
You may use words of the list as many times as needed.
If it is impossible to construct the string, return -1.
def quickest_concat(s, words):
memo = {}
result = _quickest_concat(s, words, memo)
if result == float('inf'):
return -1
else:
return result
def _quickest_concat(s, words, memo):
if not s:
return 0
if s in memo:
return memo[s]
result = float('inf')
for w in words:
if s.startswith(w):
current = 1 + _quickest_concat(s[len(w):], words, memo)
result = min(result, current)
memo[s] = result
return result
To be more specific, I don't understand the purposes of memo, float('inf'), and min(), etc, in this function.