LeetCode Problem

How to Solve Minimum Number of Valid Strings to Form Target II

This problem requires determining the minimum number of valid prefix strings to form the target string. Using state transition dynamic programming, you track the smallest concatenation count for each prefix of the target. If forming the target is impossible, the solution returns -1.

GhostInterview Help

Need help with Minimum Number of Valid Strings to Form Target II without spending extra time grinding it?

GhostInterview can read Minimum Number of Valid Strings to Form Target II from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #3292State transition dynamic programmingReviewed 2026-03-08
Difficulty
Hard
Primary pattern
State transition dynamic programming
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

This problem requires determining the minimum number of valid prefix strings to form the target string. Using state transition dynamic programming, you track the smallest concatenation count for each prefix of the target. If forming the target is impossible, the solution returns -1.

Problem Statement

Given an array of strings words and a string target, a string is considered valid if it matches a prefix of any string in words. You must compute how many valid strings must be concatenated to exactly form target.

Return the minimum number of valid strings needed. If no combination of valid strings can form target, return -1. Each string in words can be reused multiple times, and all strings contain only lowercase English letters.

Examples

Example 1

Input: words = ["abc","aaaaa","bcdef"], target = "aabcdabc"

Output: 3

The target string can be formed by concatenating:

Example 2

Input: words = ["abababab","ab"], target = "ababaababa"

Output: 2

The target string can be formed by concatenating:

Example 3

Input: words = ["abcdef"], target = "xyz"

Output: -1

Example details omitted.

Constraints

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 5 * 104
  • The input is generated such that sum(words[i].length) <= 105.
  • words[i] consists only of lowercase English letters.
  • 1 <= target.length <= 5 * 104
  • target consists only of lowercase English letters.

Solution Approach

Dynamic Programming Table

Define dp[i] as the minimum number of valid strings to form the prefix of target up to index i. Initialize dp[0] = 0 and all others to infinity. Iterate over target positions and update dp using all valid prefixes from words.

Prefix Lookup Optimization

Preprocess words into a trie or hashmap of prefixes for fast lookups. For each position in target, check all valid prefixes in O(1) to O(length of prefix) time, reducing redundant substring checks in DP updates.

Result Extraction

After filling dp, check dp[target.length]. If it remains infinity, return -1 indicating target formation is impossible. Otherwise, return dp[target.length] as the minimum concatenation count.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

Time complexity is O(N * L * W) where N is target length, L is maximum word length, and W is number of words, optimized with prefix hashing. Space complexity is O(N + total prefix storage) for DP and prefix structures.

What Interviewers Usually Probe

  • Mentions state transition dynamic programming and prefix concatenation strategy.
  • Asks about optimizing substring checks and reducing DP iteration overhead.
  • Explores edge cases where target cannot be formed or words are very long.

Common Pitfalls or Variants

Common pitfalls

  • Failing to consider all valid prefixes leads to incorrect DP updates.
  • Using naive substring comparisons results in TLE for large input sizes.
  • Returning wrong index offset or missing initial DP initialization at zero.

Follow-up variants

  • Limit reuse of words to at most once per concatenation, changing DP transitions.
  • Count all distinct ways to form target instead of minimum number.
  • Extend to allow words containing wildcards that match multiple characters.

How GhostInterview Helps

  • Identifies valid prefix combinations efficiently using DP tracking for target construction.
  • Provides quick detection when target formation is impossible, avoiding trial-and-error concatenations.
  • Guides through prefix preprocessing and optimized DP iteration for large word arrays.

Topic Pages

FAQ

What is the core pattern behind Minimum Number of Valid Strings to Form Target II?

The core pattern is state transition dynamic programming, where dp[i] tracks the minimum concatenations needed for each target prefix.

Can words be reused multiple times in forming the target?

Yes, each word can be used multiple times, but the DP ensures only valid prefix concatenations are counted.

What if no combination of words can form the target?

The DP will leave dp[target.length] as infinity, and the algorithm returns -1 to indicate impossibility.

How does prefix preprocessing help in this problem?

Preprocessing into a trie or hashmap allows fast checking of valid prefixes, reducing redundant DP checks and improving efficiency.

Is there a space-efficient version for very long targets?

Yes, you can optimize space by storing only recent dp states or compressing the prefix structure, but time complexity depends on prefix checks.

GhostInterview Solver

Need direct help with Minimum Number of Valid Strings to Form Target II instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Minimum Number of Valid Strings to Form Target II from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.