> For the complete documentation index, see [llms.txt](https://jaywin.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jaywin.gitbook.io/leetcode/solutions/1055-shortest-way-to-form-string.md).

# 1055. Shortest Way to Form String

<https://leetcode.com/problems/shortest-way-to-form-string>

## Description

A **subsequence** of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., `"ace"` is a subsequence of `"abcde"` while `"aec"` is not).

Given two strings `source` and `target`, return *the minimum number of **subsequences** of* `source` *such that their concatenation equals* `target`. If the task is impossible, return `-1`.

**Example 1:**

```
**Input:** source = "abc", target = "abcbc"
**Output:** 2
**Explanation:** The target "abcbc" can be formed by "abc" and "bc", which are subsequences of source "abc".
```

**Example 2:**

```
**Input:** source = "abc", target = "acdbc"
**Output:** -1
**Explanation:** The target string cannot be constructed from the subsequences of source string due to the character "d" in target string.
```

**Example 3:**

```
**Input:** source = "xyz", target = "xzyxz"
**Output:** 3
**Explanation:** The target string can be constructed as follows "xz" + "y" + "xz".
```

**Constraints:**

* `1 <= source.length, target.length <= 1000`
* `source` and `target` consist of lowercase English letters.

## ac

```java
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jaywin.gitbook.io/leetcode/solutions/1055-shortest-way-to-form-string.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
