Ctrl K

Finish the Word by Appending

Medium
You get two strings text and word made of lowercase English letters. You may add any letters to the end of text. Find the fewest letters you must add so that word becomes a subsequence of the result: it can be read in it from left to right, possibly skipping other letters in between.
Example 1
text="planet", word="plane" → 0

The letters p, l, a, n, e already appear in text in the right order, so nothing needs to be added.

Example 2
text="banana", word="bandana" → 4

Only "ban" can be taken from text, because no d follows that n. Appending "dana" gives "bananadana", where "bandana" reads as b, a, n from the start and d, a, n, a from the tail.

Example 3
text="xyz", word="cab" → 3

text has no c at all, so the whole word has to be appended.

Constraints
  • 1 ≤ len(text), len(word) ≤ 10⁵
  • text and word contain only lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPieces Without Shared Letters MediumA Note From Headline Cutouts MediumGreatest Interleave of Two Words MediumSwaps to the k-th Next Arrangement MediumSwapping Brackets Into Balance Medium
loading Python…
Input
text="planet", word="plane"
Expected
0

Run checks the first 3 tests in your browser. Submit checks all 149 tests on the server, hidden ones included.