Watch 4 video solutions for Minimum Number of Valid Strings to Form Target I, a medium level problem involving Array, String, Binary Search. This walkthrough by Ayush Rao has 1,843 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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: 3
Explanation:
The target string can be formed by concatenating:
words[1], i.e. "aa".words[2], i.e. "bcd".words[0], i.e. "abc".Example 2:
Input: words = ["abababab","ab"], target = "ababaababa"
Output: 2
Explanation:
The target string can be formed by concatenating:
words[0], i.e. "ababa".words[0], i.e. "ababa".Example 3:
Input: words = ["abcdef"], target = "xyz"
Output: -1
Constraints:
1 <= words.length <= 1001 <= words[i].length <= 5 * 103sum(words[i].length) <= 105.words[i] consists only of lowercase English letters.1 <= target.length <= 5 * 103target consists only of lowercase English letters.Problem Overview: You are given a list of valid strings and a target. The goal is to build the target by concatenating prefixes of these valid strings. Each chosen segment must match a prefix of some string in the list. Return the minimum number of segments required to construct the full target, or -1 if it cannot be formed.
Approach 1: Recursive Backtracking (Exponential Time, O(2^n) time, O(n) space)
This approach tries every possible way to build the target starting from index 0. At each position, iterate through all valid strings and check whether any prefix of that string matches the current portion of the target. If it matches, recursively continue building the target from the next index. Track the minimum number of segments used. Because the recursion explores many overlapping subproblems, the time complexity grows exponentially in the worst case. Space complexity is O(n) from the recursion stack. This approach demonstrates the core idea but becomes slow for large inputs.
Approach 2: Dynamic Programming with Prefix Matching (O(n * L) time, O(n) space)
A more efficient strategy uses dynamic programming. Define dp[i] as the minimum number of valid segments needed to construct the prefix target[0:i]. Initialize dp[0] = 0 and iterate through the target string. For each index i, attempt to match prefixes of all valid strings starting at that position. If a prefix matches target[i:j], update dp[j] with min(dp[j], dp[i] + 1). Efficient prefix checks can be accelerated using structures like a trie or techniques from string matching. The DP table ensures each prefix is solved once, reducing redundant work.
Approach 3: DP with Trie Optimization (O(n * k) time, O(T) space)
When the list of valid strings is large, building a Trie helps speed up prefix lookups. Insert all valid strings into the trie. For each starting index i in the target, walk through the trie while scanning characters forward in the target. Every time you reach a trie node that represents a valid prefix boundary, update dp[j]. This removes repeated string comparisons and limits checks to feasible prefixes only. Time complexity becomes roughly O(n * k), where k is the maximum prefix length explored, while space depends on the trie size.
Recommended for interviews: Start by explaining the recursive idea to show understanding of the search space. Then transition to the dynamic programming solution, which eliminates overlapping work. Interviewers usually expect the DP approach, often combined with a trie or optimized prefix checking, since it demonstrates strong control of string processing, DP state transitions, and scalable design.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Recursive Backtracking | O(2^n) | O(n) | Understanding the brute force search and validating all possible segmentations |
| Dynamic Programming | O(n * L) | O(n) | General solution for most constraints with predictable performance |
| DP with Trie Optimization | O(n * k) | O(T) | Large dictionary of strings where fast prefix lookup improves performance |