Shortest Way to Form String - Problem

Imagine you have a source cookbook and you want to create a target recipe by copying ingredients in order from the cookbook pages. You can flip through the cookbook multiple times, but you must follow the ingredient order on each page.

Given two strings source and target, find the minimum number of passes through the source string needed to form the target string as a concatenation of subsequences.

A subsequence maintains the original character order but allows skipping characters. For example, "ace" is a subsequence of "abcde", but "aec" is not.

Goal: Return the minimum number of subsequences of source that concatenate to form target. If impossible, return -1.

Example: If source = "abc" and target = "abcbc", we need 2 passes: "abc" + "bc" = "abcbc"

Input & Output

example_1.py โ€” Basic Case
$ Input: source = "abc", target = "abcbc"
โ€บ Output: 2
๐Ÿ’ก Note: We need 2 subsequences: "abc" (taking all characters) + "bc" (skipping 'a'). Total: "abc" + "bc" = "abcbc"
example_2.py โ€” Impossible Case
$ Input: source = "abc", target = "aabbcc"
โ€บ Output: -1
๐Ÿ’ก Note: Source contains only one 'a', but target needs two 'a's. Since we can't create multiple copies of the same character from a single source, it's impossible.
example_3.py โ€” Greedy Optimization
$ Input: source = "xyz", target = "xzyxz"
โ€บ Output: 3
๐Ÿ’ก Note: First pass: "xz" (skip 'y'), second pass: "y", third pass: "xz" again. We get "xz" + "y" + "xz" = "xzyxz"

Visualization

Tap to expand
๐Ÿณ The Recipe Book ChallengeCookbook (Source): "abcabc"๐ŸŽa(0)๐Ÿฅ–b(1)๐Ÿง€c(2)๐ŸŽa(3)๐Ÿฅ–b(4)๐Ÿง€c(5)Quick Index:๐ŸŽ Apple: [0, 3]๐Ÿฅ– Bread: [1, 4]๐Ÿง€ Cheese: [2, 5]Target Recipe: "abcbc"๐ŸŽ๐Ÿฅ–๐Ÿง€๐Ÿฅ–๐Ÿง€๐Ÿ“– Reading 1 (Greedy Collection):Collect: ๐ŸŽ(0) โ†’ ๐Ÿฅ–(1) โ†’ ๐Ÿง€(2)Result: "abc" โœ“๐Ÿ“– Reading 2 (Remaining Items):Collect: ๐Ÿฅ–(4) โ†’ ๐Ÿง€(5)Result: "bc" โœ“๐ŸŽฏ Final Recipe: "abc" + "bc" = "abcbc"โœ… Success with 2 readings!Binary search optimization makes each ingredient lookup O(log n) instead of O(n)
Understanding the Visualization
1
Index the Cookbook
Create a quick-reference index showing where each ingredient appears
2
Greedy Reading
For each needed ingredient, find the next occurrence in the current reading
3
New Reading When Needed
When an ingredient isn't available ahead, start a fresh reading
4
Count Total Readings
The number of readings equals the minimum subsequences needed
Key Takeaway
๐ŸŽฏ Key Insight: Pre-indexing character positions + binary search + greedy collection strategy minimizes the number of source readings required to form any target sequence.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(m * n)

For each character in target (m), we might scan the entire source (n) in worst case

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using a few integer variables for tracking positions

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค source.length, target.length โ‰ค 1000
  • source and target consist of lowercase English letters
  • Follow-up: Can you optimize for the case where the same source is queried with multiple different targets?
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
89.2K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen