Given a string s and an array of strings words, return the number of words[i] that is a subsequence of s.
A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.
"ace" is a subsequence of "abcde". Input: s = "abcde", words = ["a","bb","acd","ace"]
Output: 3
Explanation: There are three strings in words that are a subsequence of s: "a", "acd", "ace".
Input: s = "dsahjpjauf", words = ["ahjpjau","ja","ahbwzgqnuk","tnmlanowax"]
Output: 2
s and words[i] consist of only lowercase English letters.The brute force approach involves checking if each word in the list of words is a subsequence of the string s by iterating over both the string and the word.
For each word, use two pointers to check if it's a subsequence of s. One pointer iterates over the string s and the other iterates over the current word. By using these pointers, we can check if all characters of the word appear in the same order in s.
n is the length of the string s and m is the total length of all words combined.Instead of checking each word one by one, we can preprocess the string s to speed up the lookup of subsequence verification. We record the indices of each character in s, and for each word, we use this information to see if the characters appear in order.
Create a mapping from each character in s to the list of indices where that character occurs. For each word, check each character in order, using binary search to find a valid position for the next character.
n is the length of the string s, m is the total length of all words combined, and k is the maximum length of indices lists.This approach involves using a binary search strategy to locate the next character of each word in s, optimizing the search for characters using a mapping similar to the Efficient Dictionary Approach.
For each word, leverage binary search to efficiently decide if the sequence of characters can be found in order by navigating the index list of each character in s.
n is the length of s, m is the total length of the words, and k is the length of the indices list for a character.