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
Inputwords = ["abc","aaaaa","bcdef"], target = "aabcdabc"
Output3
The target string can be formed by concatenating "aa", "bcd", and "abc", each of which is a prefix of a string in words.
Example 2
Inputwords = ["abababab","ab"], target = "ababaababa"
Output2
The 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.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate