FastPrepDetermine Minimum Characters to Append

Determine Minimum Characters to Append

Amazon logoAmazon● EasyOA
Learn

Problem statement

You are given two strings, searchWord and resultWord. You may append characters only to the end of searchWord. Return the minimum number of characters that must be appended so that resultWord is a subsequence of the resulting searchWord.

A subsequence is formed by deleting zero or more characters without changing the order of the remaining characters. Equivalently, match the longest prefix of resultWord as a subsequence of searchWord; the unmatched suffix of resultWord is what must be appended.

Function

minCharactersToAppend(searchWord: String, resultWord: String) → int

Examples

Example 1

searchWord = "abcz"resultWord = "azdb"return = 2

The prefix az of resultWord appears in order in searchWord at indices 0 and 3. The unmatched suffix is db, whose length is 2. Appending it produces abczdb, which contains azdb as a subsequence.

Constraints

  • 1 <= searchWord.length, resultWord.length <= 100000
  • Both strings contain only lowercase English letters.

More Amazon problems

See Amazon hiring insights
public int minCharactersToAppend(String searchWord, String resultWord) {
  // write your code here
}
searchWord"abcz"
resultWord"azdb"
expected2
Checking account…