Advertisement

Number of Matching Subsequences - LeetCode 792 Solution

Number of Matching Subsequences - Complete Solution Guide

Number of Matching Subsequences is LeetCode problem 792, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given a string s and an array of strings words , return the number of words[i] that is a subsequence of s . A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters. For example, "ace" is a subsequence of "abcde" . Example 1: Input: s = "abcde", words = ["a","bb","acd","ace"] Output: 3 Explanation: There are three strings in words that are a subsequence of s: "a", "acd",

Detailed Explanation

The problem asks us to find the number of strings in a given array of strings (`words`) that are subsequences of another given string (`s`). A subsequence is formed by deleting zero or more characters from the original string without changing the relative order of the remaining characters. For instance, "ace" is a subsequence of "abcde". The inputs are the string `s` and the array of strings `words`. The output is an integer representing the count of strings in `words` that are subsequences of `s`. The constraints specify the lengths of the strings and arrays, limiting the size of the problem.

Solution Approach

The solution uses a hash table (specifically, a `defaultdict` in Python or a `HashMap` in Java/C++) to group the words based on their current expected character. Initially, words are grouped based on their first character. The algorithm iterates through the characters of the string `s`. For each character in `s`, it checks if there are any words waiting for that character in the hash table. If found, it processes those words by advancing their iterators to the next character they need to match. If a word's iterator reaches the end, it means the entire word is a subsequence of `s`, and the count is incremented. Words that are still subsequences get re-added to the hash table under the next expected character.

Step-by-Step Algorithm

  1. Step 1: Create a hash table (or dictionary) called `waiting` to store lists of iterators, keyed by the character they're waiting for.
  2. Step 2: Iterate through the `words` array. For each word, create an iterator and add it to the `waiting` list corresponding to its first character.
  3. Step 3: Initialize a counter `count` to 0. This will keep track of the number of subsequences found.
  4. Step 4: Iterate through the string `s`. For each character `char` in `s`:
  5. Step 5: Get the list of iterators waiting for `char` from the `waiting` hash table. Remove this entry from the table.
  6. Step 6: Iterate through the retrieved list of iterators. For each iterator `it`:
  7. Step 7: Advance the iterator `it` to its next character. If `it` reaches the end of the word (i.e., no next character), increment `count` because the current word is a subsequence of `s`.
  8. Step 8: If `it` has a next character, add it to the `waiting` list associated with that character.
  9. Step 9: After iterating through the entire string `s`, return the `count`.

Key Insights

  • Insight 1: Iterating through the main string `s` and efficiently checking if each word in `words` is a subsequence is crucial.
  • Insight 2: Using iterators to track the current position in each word can help efficiently verify the subsequence property.
  • Insight 3: Employing a hash table (or dictionary) to group words by their starting characters allows for efficient processing during iteration of `s`.

Complexity Analysis

Time Complexity: O(S+W*L)

Space Complexity: O(W*L)

Topics

This problem involves: Array, Hash Table, String, Binary Search, Dynamic Programming, Trie, Sorting.

Companies

Asked at: Salesforce, Visa.