Count Subsequence Occurrences with Different Characters

Last Updated : 22 May, 2026

Given two strings s and w, find the number of times w appears as a subsequence of string s where every character of string s can be included in forming at most one subsequence.

Examples :

Input: s = "abcdeacbced", w = "bcd" 
Output: 2
Explanation: The two subsequences of string w are highlighted in abcdeabced. The first subsequence is formed by (s[1], s[2] and s[3]) and second by (s[7], s[8] and s[10])

Input: s = "abcde", w = "fgh" 
Output: 0
Explanation: No valid subsequences are possible. 

Try It Yourself
redirect icon

The idea is to repeatedly try to form the subsequence w. Whenever a character from s is used in a subsequence, mark it so it cannot be reused again. Continue this process until no more subsequences can be formed.

Let us understand with an example:
Input: s = "abcdeacbced", w = "bcd"
Initialize res = 0.
Traverse the string and try to form the subsequence "bcd".

  • Ignore 'a'
  • Match 'b', 'c' and 'd'
  • Mark them as '*'

Updated string: a***eacbced
One subsequence is formed, so increment result: res = 1
Again traverse the string to form another "bcd" subsequence.

  • Ignore already marked characters
  • Match next 'b', 'c' and 'd'
  • Mark them as '*'

Updated string: a***eac**e*
Another subsequence is formed: res = 2
Traverse again, No more valid "bcd" subsequences can be formed.
Final result: 2

C++
#include <bits/stdc++.h>
using namespace std;

int numOfSubseq(string &s, string &w)
{
    int res = 0;

    // Until no such subsequence exists
    while (true)
    {
        int i = 0, j = 0;
        bool flag = 0;
        while (i < s.size())
        {
            if (s[i] == w[j])
            {
                ++j;
                s[i] = '*';

                //  If a subsequence is found
                if (j == w.size())
                {
                    ++res;
                    flag = true;
                    break;
                }
            }
            ++i;
        }

        // No subsequence found in this iteration
        if (!flag)
            break;
    }
    return res;
}

int main()
{
    string s = "abcdeacbced";
    string w = "bcd";

    cout << numOfSubseq(s, w) << endl;

    return 0;
}
C
#include <stdio.h>
#include <string.h>

int numOfSubseq(char *s, char *w)
{
    int res = 0;

    // Until no such subsequence exists
    while (1)
    {
        int i = 0, j = 0;
        int flag = 0;
        while (i < strlen(s))
        {
            if (s[i] == w[j])
            {
                ++j;
                s[i] = '*';

                //  If a subsequence found
                if (j == strlen(w))
                {
                    ++res;
                    flag = 1;
                    break;
                }
            }
            ++i;
        }

        // No subsequence found in this iteration
        if (!flag)
            break;
    }
    return res;
}

int main()
{
    char s[] = "abcdeacbced";
    char w[] = "bcd";

    printf("%d\n", numOfSubseq(s, w));

    return 0;
}
Java
public class GfG {
    public static int numOfSubseq(String s, String w)
    {
        int res = 0;
        char[] a = s.toCharArray();

        while (true) {
            int i = 0, j = 0;
            boolean flag = false;

            while (i < a.length) {
                if (a[i] == w.charAt(j)) {
                    j++;
                    a[i] = '*';

                    if (j == w.length()) {
                        res++;
                        flag = true;
                        break;
                    }
                }
                i++;
            }

            if (!flag)
                break;
        }

        return res;
    }

    public static void main(String[] args)
    {
        String s = "abcdeacbced";
        String w = "bcd";

        System.out.println(numOfSubseq(s, w));
    }
}
Python
def numOfSubseq(s, w):
    res = 0

    # Until no such subsequence exists
    while True:
        i = 0
        j = 0
        flag = False
        s = list(s)

        while i < len(s):
            if s[i] == w[j]:
                j += 1
                s[i] = '*'

                # If a subsequence found
                if j == len(w):
                    res += 1
                    flag = True
                    break
            i += 1

        # No subsequence found in this iteration
        if not flag:
            break

    return res

if __name__ == '__main__':
    s = "abcdeacbced"
    w = "bcd"

    print(numOfSubseq(s, w))
C#
using System;

public class GfG {
    
    public int numOfSubseq(string s, string w)
    {
        int res = 0;
        char[] a = s.ToCharArray();

        while (true) {
            
            int i = 0, j = 0;
            bool flag = false;

            while (i < a.Length) {
                
                if (a[i] == w[j]) {
                    
                    j++;
                    a[i] = '*';

                    // If a subsequence found
                    if (j == w.Length) {
                        res++;
                        flag = true;
                        break;
                    }
                }
                
                i++;
            }

            // No subsequence found
            if (!flag)
                break;
        }

        return res;
    }

    public static void Main()
    {
        string s = "abcdeacbced";
        string w = "bcd";

        GfG obj = new GfG();

        Console.WriteLine(obj.numOfSubseq(s, w));
    }
}
JavaScript
function numOfSubseq(s, w)
{
    let res = 0;

    // Until no such subsequence exists
    while (true) {
        let i = 0, j = 0;
        let flag = false;
        while (i < s.length) {
            if (s[i] === w[j]) {
                ++j;
                s[i] = "*";

                // If a subsequence found
                if (j === w.length) {
                    ++res;
                    flag = true;
                    break;
                }
            }
            ++i;
        }

        // No subsequence found in this iteration
        if (!flag)
            break;
    }
    return res;
}

let s = "abcdeacbced";
let w = "bcd";

console.log(numOfSubseq(s.split(""), w.split("")));

Output
2

Time Complexity: O(n^2)
Auxiliary Space: O(1)

Comment