Program to find number of subsequence that are present inside word list in python

Given a list of words and a string s, we need to find how many words are subsequences of s. A subsequence maintains the relative order of characters but doesn't need to be contiguous.

So, if the input is like words = ["xz", "xw", "y"] and s = "xyz", then the output will be 2, as "xz" and "y" are subsequences of "xyz".

Algorithm

To solve this, we will follow these steps ?

  • Initialize ans := 0 and create an empty dictionary d
  • Group words by their first character in dictionary d
  • For each character c in string s:
    • Get all words starting with c
    • For each word, if it has only one character, increment ans
    • Otherwise, remove the first character and add to appropriate group
  • Return ans

Implementation

from collections import defaultdict

class Solution:
    def solve(self, words, s):
        ans = 0
        
        # Dictionary to group words by their first character
        d = defaultdict(list)
        for word in words:
            d[word[0]].append(word)
        
        # Process each character in string s
        for c in s:
            current_words = d[c]
            d[c] = []  # Clear current group
            
            for word in current_words:
                if len(word) == 1:
                    ans += 1  # Found complete subsequence
                else:
                    # Add remaining part to next character group
                    d[word[1]].append(word[1:])
        
        return ans

# Test the solution
ob = Solution()
words = ["xz", "xw", "y"]
s = "xyz"
result = ob.solve(words, s)
print(f"Number of subsequences: {result}")
Number of subsequences: 2

How It Works

Let's trace through the example with words = ["xz", "xw", "y"] and s = "xyz":

  1. Initial grouping: d = {'x': ["xz", "xw"], 'y': ["y"]}
  2. Process 'x': Remove "xz", "xw" from d['x'], add "z" to d['z'], "w" to d['w']
  3. Process 'y': Find "y" in d['y'], increment ans to 1
  4. Process 'z': Find "z" in d['z'], increment ans to 2

Alternative Approach

Here's a simpler approach that checks each word individually:

def count_subsequences(words, s):
    def is_subsequence(word, string):
        i = 0  # pointer for word
        for char in string:
            if i < len(word) and char == word[i]:
                i += 1
        return i == len(word)
    
    count = 0
    for word in words:
        if is_subsequence(word, s):
            count += 1
    
    return count

# Test the alternative approach
words = ["xz", "xw", "y"]
s = "xyz"
result = count_subsequences(words, s)
print(f"Number of subsequences: {result}")
Number of subsequences: 2

Comparison

Method Time Complexity Space Complexity Best For
Dictionary Grouping O(n + m) O(n) Multiple queries
Individual Check O(n × m) O(1) Simple implementation

Where n is the total length of all words and m is the length of string s.

Conclusion

The dictionary grouping approach efficiently processes all words simultaneously by organizing them by their current character. The alternative approach is simpler to understand but less efficient for multiple subsequence checks.

Updated on: 2026-03-25T12:58:56+05:30

448 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements