All problems
0984MediumArrayHash TableStringBinary SearchDynamic ProgrammingTrieSorting

Labels That Read Through the Tape

Tracked in this browser only
Write code

Trains the technique from

LeetCode 792Number of Matching Subsequences

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A tape reads s, and a list of labels is given as words.

A label reads through the tape when its characters can be found on the tape in order, though not necessarily next to each other.

Return how many of the labels read through the tape, counting a label once for each time it appears in the list.

Examples

Example 1

Input
s = "abcbadcbc", words = ["abc", "acb", "bcd", "cba", "abcd", "zz"]
Output
5

Four of the six read through: "abc", "acb", "bcd" and "cba" can each be found in order along the tape. The label "abcd" cannot, and "zz" has no letters on the tape at all.

Example 2

Input
s = "abc", words = ["abc", "abc", "abc"]
Output
3

The same label appears three times and each occurrence counts.

Example 3

Input
s = "a", words = ["aa"]
Output
0

The tape holds only one a, so a label needing two cannot read through.

Constraints

  • 1 <= s.length <= 5 * 10^4
  • 1 <= words.length <= 5000
  • 1 <= words[i].length <= 50
  • s and every entry of words consist of lowercase English letters only

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def num_matching_subseq(s: str, words: list[str]) -> int:
Java
public int numMatchingSubseq(String s, String[] words)
September 7
Apply