Leetcode 1055. Shortest Way to Form String
Given a source and target string, return the minimum number of subsequences of source that need to be concatenated to form target (or -1 if any target character doesn't appear in source). The core challenge is to cover the target by repeatedly taking ordered subsequences from source and minimizing the number of passes.
Asked at:
Question Timeline
See when this question was last asked and where, including any notes left by other candidates.
Early July, 2026
Mid-level
Original requirement: return boolean if target can be created from source, follow up: return minimum subsequences to create target from source
Late June, 2026
Staff
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".
Hello Interview Premium
Your account is free and you can post anonymously if you choose.