Append Characters to String to Make Subsequence

You are given two strings `s` and `t` of 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.

30:00

Append Characters to String to Make Subsequence
medium
Free written solution
Topics
Companies

You are given two strings s and t of 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:
Input: {"s":"coaching","t":"coding"}
Output: 4
Constraints:
  • 1≤s.length,t.length≤1051 \leq s.\text{length}, t.\text{length} \leq 10^5

  • s and t consist only of lowercase English letters.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"s":"coaching","t":"coding"}

j = 0 points at t[0] = 'c', the next letter of t we still need to find in s.

s, scanned once by i (green = used for a letter of t)

0

c

1

o

2

a

3

c

4

h

5

i

6

n

7

g

t, j = letters matched in order

j

c

1

o

2

d

3

i

4

n

5

g

i = -
j = 0 / 6
still missing = 6
Variables
VariableValue
j0
len(t)6
DepthFunction Call
Stack empty
0/10