Can Construct
Updated: 01 February 2024
Problem
Given a target string and a list of strings return a boolean that indicates if the items in the string list can be combined to build the string in the target
An example of what we want to do is:
In general it will probably be easier to create a shorter string than a longer one
We can consider a base case where:
Base Implementation
We can work on shrinking the string by stepping and splitting off subscrings in our input data ensuring that we do not break the sequencing of the initial string, so we do not remove any segments from the middle of the string
We will split off substrings if they are within the start of our initial string
In this implementation the height of this tree can be the length of the target word , for every element we would do a check which is , in this case the time complexity is (the last because we’re slicing the string on each iteration). The space complexity is the at most the length of our string of and the new string we have that is maybe also of length , the space complexity is the
With memoization
We implement the memoization as in the previous examples
When we memoize this we are removing subtrees, this way we cut down the width of our tree. In the act of memoizing we define a map that stores the result of each substring which is of size , the overall time complexity is the and a space complexity of