Append Characters to String to Make Subsequence

You are given two strings s and t consisting of only lowercase English letters.

Return the minimum number of characters that need to be appended to the end of s so that t becomes a subsequence of s.

A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.

Example 1
Inputs = "coaching", t = "coding"
Output4
Append the characters "ding" to the end of s so that s = "coachingding"; then t is a subsequence of s, and any 3 appended characters are not enough.
Example 2
Inputs = "abcde", t = "a"
Output0
t is already a subsequence of s.

Constraints

  • 1 <= s.length, t.length <= 10^5
  • s and t consist only of lowercase English letters.

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate