Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Number of Matching Subsequences

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "792" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

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", "ace". Example 2: Input: s = "dsahjpjauf", words = ["ahjpjau","ja","ahbwzgqnuk","tnmlanowax"] Output: 2 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.

Explanation

Here's an efficient solution to the subsequence counting problem:

  • Precompute Character Indices: Create a dictionary where keys are characters from s and values are lists of their indices in s. This allows for quick lookup of character positions.
  • Iterate and Check Subsequence: Iterate through each word in words. For each word, use the precomputed indices to efficiently check if it's a subsequence of s. Start with an index of -1. For each character in word, look for the next index of the character in s after the current index. if character is not found, the word is not a subsequence.
  • Count Subsequences: Increment a counter for each word that is a subsequence of s.

  • Runtime & Storage Complexity: O(S + W * L), where S is the length of s, W is the number of words, and L is the maximum length of a word. The storage complexity is O(S) due to storing indices of characters in s.

Code

    def num_matching_subseq(s: str, words: list[str]) -> int:
    """
    Given a string s and an array of strings words, return the number of words[i]
    that is a subsequence of s.
    """

    char_indices = {}
    for i, char in enumerate(s):
        if char not in char_indices:
            char_indices[char] = []
        char_indices[char].append(i)

    count = 0
    for word in words:
        current_index = -1
        is_subsequence = True
        for char in word:
            if char not in char_indices:
                is_subsequence = False
                break

            indices = char_indices[char]
            next_index = -1
            for index in indices:
                if index > current_index:
                    next_index = index
                    break

            if next_index == -1:
                is_subsequence = False
                break

            current_index = next_index

        if is_subsequence:
            count += 1

    return count

More from this blog

C

Chatmagic blog

2894 posts