Number of Matching Subsequences - Problem

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

example_1.py โ€” Basic Example
$ Input: s = "abcde", words = ["a", "bb", "acd", "ace"]
โ€บ Output: 3
๐Ÿ’ก Note: The subsequences are "a" (matches s[0]), "acd" (matches s[0], s[2], s[3]), and "ace" (matches s[0], s[2], s[4]). The word "bb" is not a subsequence because there's only one 'b' in s.
example_2.py โ€” All Match
$ Input: s = "dsahjpjauf", words = ["ahjpjau", "ja", "ahbwzgqnuk", "tnmlanowax"]
โ€บ Output: 2
๐Ÿ’ก Note: "ahjpjau" is a subsequence (matches positions 2,3,4,5,6,7,8), and "ja" is a subsequence (matches positions 3,8). The other words cannot be formed as subsequences.
example_3.py โ€” Edge Cases
$ Input: s = "wordgoodgoodgoodbestword", words = ["word", "good", "best", "good"]
โ€บ Output: 3
๐Ÿ’ก Note: "word" appears at the beginning, "good" can be formed multiple times (but we count each occurrence in words array), and "best" appears in the middle. Note that "good" appears twice in the words array, so it contributes 2 to the count.

Visualization

Tap to expand
Word Journey Through StringPlatform A"a""acd""ace"Platform B"bb"Platform C(empty)CompletedCount: 0Train'a'Words board trainProcessing: "abcde"abcdeStep 1: Character 'a' arrives - words "a", "acd", "ace" board the train"a" completes journey โœ“, "acd" and "ace" move to Platform C
Understanding the Visualization
1
Setup Platforms
Create 26 platforms (a-z) and assign each word to the platform of its first character
2
Train Arrives
When character 'a' appears in string, all words on platform 'a' board the train
3
Next Destination
Words advance to their next character's platform, or exit if journey complete
4
Count Completions
Count how many words successfully completed their journey through the string
Key Takeaway
๐ŸŽฏ Key Insight: By grouping words by their expected character and processing them together, we eliminate redundant string scans and achieve optimal O(n + m) performance.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n + m)

O(n) to scan through string s once, O(m) to process all characters in all words exactly once

n
2n
โœ“ Linear Growth
Space Complexity
O(m)

Space to store word iterators in buckets, proportional to total length of all words

n
2n
โœ“ Linear Space

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
Asked in
Google 45 Amazon 38 Meta 28 Microsoft 22 Apple 15
68.3K Views
High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen