Problem · String

Determine Minimum Characters to Append

Learn this problem
EasyAmazon logoAmazonOA
See Amazon hiring insights

Problem statement

Given two strings, searchWord and resultWord:

Determine the minimum number of characters you need to append to resultWord so that it becomes a subsequence of searchWord.

Example:

Input: searchWord = "abcz" resultWord = "azdb"

Output: 2

Explanation: By appending 'd' and 'b' to "azdb", we can transform it into a subsequence of "abcz".

Understanding the Problem:

A subsequence of a string is derived by deleting some or no characters from it without changing the order of the remaining characters. For example:

"ace" is a subsequence of "abcde" because 'a', 'c', and 'e' appear in order.

"ace" is NOT a subsequence of "abcde" because 'e' appears before 'c'.

In this problem:

Input: Two strings, searchWord and resultWord.

Output: The number of characters to append to resultWord so that all characters in resultWord appear in order as a subsequence in searchWord.

Function

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

Examples

Example 1

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

By appending 'd' and 'b' to "azdb", we can transform it into a subsequence of "abcz".

Constraints

🐇

More Amazon problems

drafts saved locally
public int minCharactersToAppend(String searchWord, String resultWord) {
  // write your code here
}
searchWord"abcz"
resultWord"azdb"
expected2
checking account