This is a premium problem. We're working on making it available for free soon.
Use these hints if you're stuck. Try solving on your own first.
Which conditions have to been met in order to be impossible to form the target string?
If there exists a character in the target string which doesn't exist in the source string then it will be impossible to form the target string.
Assuming we are in the case which is possible to form the target string, how can we assure the minimum number of used subsequences of source?
For each used subsequence try to match the leftmost character of the current subsequence with the leftmost character of the target string, if they match then erase both character otherwise erase just the subsequence character whenever the current subsequence gets empty, reset it to a new copy of subsequence and increment the count, do this until the target sequence gets empty. Finally return the count.
Solutions for this premium problem will be available for free soon.
Browse Free ProblemsWatch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
The greedy strategy works because extending the subsequence as much as possible in each pass of the source always minimizes the number of passes needed. If a character cannot be matched at all, it immediately indicates the target cannot be formed.
Yes, this problem is commonly asked in technical interviews because it tests understanding of greedy strategies, string processing, and optimization using binary search. Variations of subsequence construction problems appear frequently in top tech interviews.
A hash map or array mapping each character to a list of its indices in the source string works well. This structure enables fast binary search lookups to find the next occurrence needed to continue building the target.
A common optimal approach preprocesses the source string by storing indices of each character. For each character in the target, binary search is used to find the next valid index in the source, minimizing repeated scans and improving efficiency.