Given a string s and an array of strings words, your task is to determine how many words in the array are subsequences of the main string s.
A subsequence is formed by deleting some (possibly zero) characters from the original string while maintaining the relative order of the remaining characters. For example, "ace" is a subsequence of "abcde" because we can obtain it by removing characters 'b' and 'd'.
Goal: Count how many words from the given array are subsequences of the main string.
Example: If s = "abcde" and words = ["a", "bb", "acd", "ace"], then "a", "acd", and "ace" are subsequences of s, so the answer is 3.
Input & Output
Visualization
Time & Space Complexity
O(n) to scan through string s once, O(m) to process all characters in all words exactly once
Space to store word iterators in buckets, proportional to total length of all words
Constraints
- 1 โค s.length โค 5 ร 104
- 1 โค words.length โค 5000
- 1 โค words[i].length โค 50
- s and words[i] consist of only lowercase English letters