#2486
Append Characters to String to Make Subsequence
pupil · 525 · lc medium +28 · verified · 73% accepted · 1,187 likes · top 83%
Description
Given two strings s and t of lowercase English letters, return the minimum number of characters that must be appended to the end of s so that t becomes a subsequence of the resulting string.
A subsequence is obtained by deleting zero or more characters from a string without changing the relative order of the remaining characters.
Example 1:
Input: s = "coaching", t = "coding"
Output: 4
Explanation: Append the characters "ding" to the end of s so that s = "coachingding".
Now, t is a subsequence of s ("coachingding").
It can be shown that appending any 3 characters to the end of s will never make t a subsequence.
Example 2:
Input: s = "abcde", t = "a"
Output: 0
Explanation: t is already a subsequence of s ("abcde").
Example 3:
Input: s = "z", t = "abcde"
Output: 5
Explanation: Append the characters "abcde" to the end of s so that s = "zabcde".
Now, t is a subsequence of s ("zabcde").
It can be shown that appending any 4 characters to the end of s will never make t a subsequence.
Code
1
2
3