Append the fewest characters to the end of one string so another string becomes its subsequence.
Given two strings s and t, you may append characters only to the end of s. Determine the minimum number of characters that must be appended so that t becomes a subsequence of the resulting string.
A string x is a subsequence of a string y if x can be formed by deleting zero or more characters from y without changing the order of the remaining characters.
s and t.s so that t is a subsequence of the final string.0 <= |s|, |t|.Example 1
Input
s = "coaching", t = "coding"
Output
4
Explanation
The longest prefix of t that appears as a subsequence of s is "co". The remaining 4 characters "ding" must be appended.
Example 2
Input
s = "abcde", t = "aebdc"
Output
3
Explanation
The subsequence match can cover "ab" from t, leaving "dc"? Wait, because order must be preserved. The longest matched prefix is "ae"? No. For this input, the minimum appended characters is 3 because only the prefix "ab" can be matched before t runs ahead.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.