Count Construct
Brute force
def count_construct_brute(
target: str,
word_bank: list[str]
) -> int:
"""
N = length of word_bank
M = length of target
Time complexity: O(N^M * M)
Worst case scenario, it will need to check every
combination of every value of N while decreasing M
(worst scenario will be by 1 character every time).
As well, in every iteration we copy and remove some
characters the target (depending on M).
Spacial complexity: O(M*M)
It will make up to M recursive calls. And for every call,
it will store the suffix of the target.
"""
if target == '':
return 1
counter = 0
for word in word_bank:
if word in target and target.index(word) == 0:
suffix = target.removeprefix(word)
counter += count_construct_brute(suffix, word_bank)
return counterMemoization
Tabulation
Last updated
Was this helpful?