Count how many words are subsequences of a string

Quick Overview

Count how many strings in a word list are subsequences of a longer string, with up to 5,000 words and a source string of up to 50,000 characters. It tests efficient subsequence matching and avoiding a full rescan of the long string for each word.

Count how many words are subsequences of a string

Company: Oracle

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a string `s` and an array of strings `words`, return the number of entries `words[i]` that are subsequences of `s`. A subsequence of a string is a new string formed from the original by deleting some characters (possibly none) without changing the relative order of the remaining characters. ### Function Signature ```python def num_matching_subseq(s: str, words: list[str]) -> int: ``` ### Rules - Each entry of `words` counts on its own. If a word appears twice in `words` and is a subsequence of `s`, it adds 2 to the answer. - A position of `s` can match at most one character of a word, so `"bb"` needs two `b` characters in `s`, in that order. ### Constraints - `1 <= len(s) <= 5 * 10^4` - `1 <= len(words) <= 5000` - `1 <= len(words[i]) <= 50` - `s` and every `words[i]` contain only lowercase English letters. ### Examples ```text Input: s = "abcde", words = ["a", "bb", "acd", "ace"] Output: 3 ``` "a", "acd" and "ace" can be read left to right inside "abcde". "bb" cannot, because `s` has only one `b`. ```text Input: s = "dsahjpjauf", words = ["ahjpjau", "ja", "ahbwzgqnuk", "tnmlanowax"] Output: 2 ``` "ahjpjau" matches positions 2 through 8 of `s`, and "ja" matches positions 4 and 7. "ahbwzgqnuk" fails because `s` has no `b`, and "tnmlanowax" fails because `s` has no `t`. ```text Input: s = "abc", words = ["ab", "ab", "ba", "c"] Output: 3 ``` Both copies of "ab" count, and so does "c". "ba" fails because no `a` comes after the `b` in `s`.

Overview: Count how many strings in a word list are subsequences of a longer string, with up to 5,000 words and a source string of up to 50,000 characters. It tests efficient subsequence matching and avoiding a full rescan of the long string for each word.

Oracle logo
Oracle
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a string s and an array of strings words, return the number of entries words[i] that are subsequences of s.

A subsequence of a string is a new string formed from the original by deleting some characters (possibly none) without changing the relative order of the remaining characters.

Function Signature

def num_matching_subseq(s: str, words: list[str]) -> int:

Rules

  • Each entry of words counts on its own. If a word appears twice in words and is a subsequence of s , it adds 2 to the answer.
  • A position of s can match at most one character of a word, so "bb" needs two b characters in s , in that order.

Constraints

  • 1 <= len(s) <= 5 * 10^4
  • 1 <= len(words) <= 5000
  • 1 <= len(words[i]) <= 50
  • s and every words[i] contain only lowercase English letters.

Examples

Input: s = "abcde", words = ["a", "bb", "acd", "ace"]
Output: 3

"a", "acd" and "ace" can be read left to right inside "abcde". "bb" cannot, because s has only one b.

Input: s = "dsahjpjauf", words = ["ahjpjau", "ja", "ahbwzgqnuk", "tnmlanowax"]
Output: 2

"ahjpjau" matches positions 2 through 8 of s, and "ja" matches positions 4 and 7. "ahbwzgqnuk" fails because s has no b, and "tnmlanowax" fails because s has no t.

Input: s = "abc", words = ["ab", "ab", "ba", "c"]
Output: 3

Both copies of "ab" count, and so does "c". "ba" fails because no a comes after the b in s.