Mid/SeniorArrayBinary SearchDynamic ProgrammingGreedyHash FunctionRolling HashSegment TreeStringString MatchingTrie
Minimum Number of Valid Strings to Form Target I
You are given an array of strings words and a string target.
A string x is called valid if x is a prefix of any string in words.
Return the minimum number of valid strings that can be concatenated to form target. If it is not possible to form target, return -1.
Example 1
Input
words = ["abc","aaaaa","bcdef"], target = "aabcdabc"Output
3The target string can be formed by concatenating "aa", "bcd", and "abc", each of which is a prefix of a string in words.
Example 2
Input
words = ["abababab","ab"], target = "ababaababa"Output
2The target string can be formed by concatenating "ababa" and "ababa", both prefixes of words[0].
Constraints
- 1 <= words.length <= 100
- 1 <= words[i].length <= 5 * 10^3
- The input is generated such that sum(words[i].length) <= 10^5.
- words[i] consists only of lowercase English letters.
- 1 <= target.length <= 5 * 10^3
- target consists only of lowercase English letters.