AlgoMaster Logo

Number of Matching Subsequences

Ashish

Ashish Pratap Singh

medium

Problem Description

Solve it on LeetCode

Approaches

1. Brute Force

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.

Intuition:

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.

Code:

2. HashMap

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.

Intuition:

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.

Code:

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.

Intuition:

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.

Code: