Program to find number of subsequences with i, j and k number of x, y, z letters in Python

When working with string subsequences, we often need to count specific patterns. This problem asks us to find the number of subsequences that contain i number of "x" characters, followed by j number of "y" characters, and then k number of "z" characters, where i, j, k ? 1.

For example, with the string "xxyz", we can form subsequences like "xyz" (twice) and "xxyz" (once), giving us a total of 3 valid subsequences.

Algorithm Approach

We use dynamic programming to track the number of valid subsequences ending with each character ?

  • x := count of subsequences ending with at least one 'x'

  • y := count of subsequences ending with 'x' followed by at least one 'y'

  • z := count of subsequences ending with 'x', 'y', and at least one 'z'

For each character in the string:

  • If current character is 'x': double existing x-subsequences and add one new

  • If current character is 'y': double existing y-subsequences and add all x-subsequences

  • If current character is 'z': double existing z-subsequences and add all y-subsequences

Implementation

def count_xyz_subsequences(s):
    n = len(s)
    
    x = 0  # Count of subsequences ending with x
    y = 0  # Count of subsequences ending with xy
    z = 0  # Count of subsequences ending with xyz
    
    for i in range(n):
        if s[i] == "x":
            x = x * 2 + 1
        elif s[i] == "y":
            y = y * 2 + x
        elif s[i] == "z":
            z = z * 2 + y
    
    return z

# Test the function
s = "xxyz"
result = count_xyz_subsequences(s)
print(f"Input: {s}")
print(f"Number of valid subsequences: {result}")
Input: xxyz
Number of valid subsequences: 3

How It Works

Let's trace through "xxyz" step by step ?

def count_xyz_subsequences_trace(s):
    x, y, z = 0, 0, 0
    
    for i, char in enumerate(s):
        print(f"Step {i+1}: Processing '{char}'")
        print(f"Before: x={x}, y={y}, z={z}")
        
        if char == "x":
            x = x * 2 + 1
        elif char == "y":
            y = y * 2 + x
        elif char == "z":
            z = z * 2 + y
            
        print(f"After: x={x}, y={y}, z={z}")
        print()
    
    return z

result = count_xyz_subsequences_trace("xxyz")
print(f"Final result: {result}")
Step 1: Processing 'x'
Before: x=0, y=0, z=0
After: x=1, y=0, z=0

Step 2: Processing 'x'
Before: x=1, y=0, z=0
After: x=3, y=0, z=0

Step 3: Processing 'y'
Before: x=3, y=0, z=0
After: x=3, y=3, z=0

Step 4: Processing 'z'
Before: x=3, y=3, z=0
After: x=3, y=3, z=3

Final result: 3

Multiple Test Cases

def test_multiple_cases():
    test_cases = ["xxyz", "xyz", "xxyyz", "xyxz", "xxxyyyzzz"]
    
    for case in test_cases:
        result = count_xyz_subsequences(case)
        print(f"String: '{case}' ? Valid subsequences: {result}")

test_multiple_cases()
String: 'xxyz' ? Valid subsequences: 3
String: 'xyz' ? Valid subsequences: 1
String: 'xxyyz' ? Valid subsequences: 6
String: 'xyxz' ? Valid subsequences: 2
String: 'xxxyyyzzz' ? Valid subsequences: 63

Conclusion

This dynamic programming approach efficiently counts subsequences by tracking the number of valid patterns ending with each character. The algorithm runs in O(n) time and uses O(1) space, making it optimal for large strings.

Updated on: 2026-03-25T11:54:42+05:30

234 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements