Dictionary Word 1
Given a word and a dictionary, determine if it can be composed by concatenating words from the given dictionary.
Assumptions
The given word is not null and is not empty
The given dictionary is not null and is not empty and all the words in the dictionary are not null or empty
Examples
Dictionary: {“bob”, “cat”, “rob”}
Word: “robob” return false
Word: “robcatbob” return true since it can be composed by "rob", "cat", "bob"
Solution: DP, incremental check string
Base Case: M[0] = true;
Induction Rule:
Check each increment
Check each cut of every increment
if left cut && right cut both are words, set current increment to true
Time complexity: O(N) increment check * O(N) cut check * O(N) String.substring Api = O(N^3)
Space Complexity: O(input size + dict size)
Last updated
Was this helpful?