Open In App

Number of subsequences of the form a^i b^j c^k

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Given a string, count number of subsequences of the form aibjck, i.e., it consists of i ’a’ characters, followed by j ’b’ characters, followed by k ’c’ characters where i >= 1, j >=1 and k >= 1. 

Note: Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
Expected Time Complexity: O(n)

Examples: 

Input  : abbc
Output : 3
Subsequences are abc, abc and abbc
Input : abcabc
Output : 7
Subsequences are abc, abc, abbc, aabc
abcc, abc and abc

Approach: 

We traverse given string. For every character encounter, we do the following:

  1. Initialize counts of different subsequences caused by different combination of ‘a’. Let this count be aCount.
  2. Initialize counts of different subsequences caused by different combination of ‘b’. Let this count be bCount.
  3. Initialize counts of different subsequences caused by different combination of ‘c’. Let this count be cCount.
  4. Traverse all characters of given string. Do following for current character s[i] 
    •     If current character is ‘a’, then there are following possibilities :O(1). 
      • Current character begins a new subsequence. 
      • Current character is part of aCount subsequences. 
      • Current character is not part of aCount subsequences. 
    •     Therefore we do aCount = (1 + 2 * aCount);
    • If current character is ‘b’, then there are following possibilities : 
      • Current character begins a new subsequence of b’s with aCount subsequences. 
      • Current character is part of bCount subsequences. 
      • Current character is not part of bCount subsequences. 
    •     Therefore we do bCount = (aCount + 2 * bCount);
    • If current character is ‘c’, then there are following possibilities : 
      • Current character begins a new subsequence of c’s with bCount subsequences. 
      • Current character is part of cCount subsequences. 
      • Current character is not part of cCount subsequences. 
    •     Therefore we do cCount = (bCount + 2 * cCount);
  5. Finally we return cCount;

Explanation of approach with help of example: 

  • aCount is the number of subsequences of the letter ‘a’.
  • Consider this example: aa.
  • We can see that aCount for this is 3, because we can choose these possibilities: (xa, ax, aa) (x means we did not use that character). Note also that this is independent of characters in between, i.e. the aCount of aa and ccbabbbcac are the same because both have exactly 2 a’s.
  • Now, adding 1 a, we now have the following new subsequences: each of the old subsequences, each of the old subsequences + the new a, and the new letter a, alone. So a total of aCount + aCount + 1 subsequences.
  • Now, let’s consider bCount, the number of subsequences with some a’s and then some b’s. in ‘aab’, we see that bCount should be 3 (axb, xab, aab) because it is just the number of ways we can choose subsequences of the first two a’s, and then b. So every time we add a b, the number of ways increases by aCount.
  • Let’s find bCount for ‘aabb’. We have already determined that aab has 3 subsequences, so certainly we still have those. Additionally, we can add the new b onto any of these subsequences, to get 3 more. Finally, we have to count the subsequences that are made without using any other b’s, and by the logic in the last paragraph, that is just aCount. So, bCount after this is just the old bCount*2 + aCount;
  • cCount is similar. 

Below is the implementation of above idea: 

C++




// C++ program to count subsequences of the
// form a^i b^j c^k
#include <bits/stdc++.h>
using namespace std;
 
// Returns count of subsequences of the form
// a^i b^j c^k
int countSubsequences(string s)
{
    // Initialize counts of different subsequences
    // caused by different combination of 'a'
    int aCount = 0;
 
    // Initialize counts of different subsequences
    // caused by different combination of 'a' and
    // different combination of 'b'
    int bCount = 0;
 
    // Initialize counts of different subsequences
    // caused by different combination of 'a', 'b'
    // and 'c'.
    int cCount = 0;
 
    // Traverse all characters of given string
    for (unsigned int i = 0; i < s.size(); i++) {
        /* If current character is 'a', then
           there are the following possibilities :
             a) Current character begins a new
                subsequence.
             b) Current character is part of aCount
                subsequences.
             c) Current character is not part of
                aCount subsequences. */
        if (s[i] == 'a')
            aCount = (1 + 2 * aCount);
 
        /* If current character is 'b', then
           there are following possibilities :
             a) Current character begins a new
                subsequence of b's with aCount
                subsequences.
             b) Current character is part of bCount
                subsequences.
             c) Current character is not part of
                bCount subsequences. */
        else if (s[i] == 'b')
            bCount = (aCount + 2 * bCount);
 
        /* If current character is 'c', then
           there are following possibilities :
             a) Current character begins a new
                subsequence of c's with bCount
                subsequences.
             b) Current character is part of cCount
                subsequences.
             c) Current character is not part of
                cCount subsequences. */
        else if (s[i] == 'c')
            cCount = (bCount + 2 * cCount);
    }
 
    return cCount;
}
 
// Driver code
int main()
{
    string s = "abbc";
    cout << countSubsequences(s) << endl;
    return 0;
}


Java




// Java program to count subsequences of the
// form a^i b^j c^k
public class No_of_subsequence {
 
    // Returns count of subsequences of the form
    // a^i b^j c^k
    static int countSubsequences(String s)
    {
        // Initialize counts of different subsequences
        // caused by different combination of 'a'
        int aCount = 0;
 
        // Initialize counts of different subsequences
        // caused by different combination of 'a' and
        // different combination of 'b'
        int bCount = 0;
 
        // Initialize counts of different subsequences
        // caused by different combination of 'a', 'b'
        // and 'c'.
        int cCount = 0;
 
        // Traverse all characters of given string
        for (int i = 0; i < s.length(); i++) {
            /* If current character is 'a', then
               there are following possibilities :
                 a) Current character begins a new
                    subsequence.
                 b) Current character is part of aCount
                    subsequences.
                 c) Current character is not part of
                    aCount subsequences. */
            if (s.charAt(i) == 'a')
                aCount = (1 + 2 * aCount);
 
            /* If current character is 'b', then
               there are following possibilities :
                 a) Current character begins a new
                    subsequence of b's with aCount
                    subsequences.
                 b) Current character is part of bCount
                    subsequences.
                 c) Current character is not part of
                    bCount subsequences. */
            else if (s.charAt(i) == 'b')
                bCount = (aCount + 2 * bCount);
 
            /* If current character is 'c', then
               there are following possibilities :
                 a) Current character begins a new
                    subsequence of c's with bCount
                    subsequences.
                 b) Current character is part of cCount
                    subsequences.
                 c) Current character is not part of
                    cCount subsequences. */
            else if (s.charAt(i) == 'c')
                cCount = (bCount + 2 * cCount);
        }
 
        return cCount;
    }
 
    // Driver code
    public static void main(String args[])
    {
        String s = "abbc";
        System.out.println(countSubsequences(s));
    }
}
// This code is contributed by Sumit Ghosh


Python 3




# Python 3 program to count
# subsequences of the form
# a ^ i b ^ j c ^ k
 
# Returns count of subsequences
# of the form a ^ i b ^ j c ^ k
def countSubsequences(s):
 
    # Initialize counts of different
    # subsequences caused by different
    # combination of 'a'
    aCount = 0
 
    # Initialize counts of different
    # subsequences caused by different
    # combination of 'a' and different
    # combination of 'b'
    bCount = 0
 
    # Initialize counts of different
    # subsequences caused by different
    # combination of 'a', 'b' and 'c'.
    cCount = 0
 
    # Traverse all characters
    # of given string
    for i in range(len(s)):
     
        # If current character is 'a',
        # then there are following
        # possibilities :
        # a) Current character begins
        # a new subsequence.
        # b) Current character is part
        # of aCount subsequences.
        # c) Current character is not
        # part of aCount subsequences.
        if (s[i] == 'a'):
            aCount = (1 + 2 * aCount)
 
        # If current character is 'b', then
        # there are following possibilities :
        # a) Current character begins a
        # new subsequence of b's with
        # aCount subsequences.
        # b) Current character is part
        # of bCount subsequences.
        # c) Current character is not
        # part of bCount subsequences.
        else if (s[i] == 'b'):
            bCount = (aCount + 2 * bCount)
 
        # If current character is 'c', then
        # there are following possibilities :
        # a) Current character begins a
        # new subsequence of c's with
        # bCount subsequences.
        # b) Current character is part
        # of cCount subsequences.
        # c) Current character is not
        # part of cCount subsequences.
        else if (s[i] == 'c'):
            cCount = (bCount + 2 * cCount)
 
    return cCount
 
# Driver code
if __name__ == "__main__":
    s = "abbc"
    print(countSubsequences(s))
 
# This code is contributed
# by ChitraNayal


C#




// C# program to count subsequences
// of the form a^i b^j c^k
using System;
 
public class GFG {
 
    // Returns count of subsequences
    // of the form a^i b^j c^k
    static int countSubsequences(String s)
    {
        // Initialize counts of different
        // subsequences caused by different
        // combination of 'a'
        int aCount = 0;
 
        // Initialize counts of different
        // subsequences caused by different
        // combination of 'a' and
        // different combination of 'b'
        int bCount = 0;
 
        // Initialize counts of different
        // subsequences caused by different
        // combination of 'a', 'b' and 'c'
        int cCount = 0;
 
        // Traverse all characters of given string
        for (int i = 0; i < s.Length; i++) {
 
            // If current character is 'a', then
            // there are following possibilities :
            // a) Current character begins a
            // new subsequence.
            // b) Current character is part
            // of aCount subsequences
            // c) Current character is not part
            // of aCount subsequences.
 
            if (s[i] == 'a')
                aCount = (1 + 2 * aCount);
 
            // If current character is 'b', then
            // there are following possibilities :
            // a) Current character begins a new
            // subsequence of b's with aCount
            // subsequences.
            // b) Current character is part of bCount
            // subsequences.
            // c) Current character is not part of
            // bCount subsequences.
            else if (s[i] == 'b')
                bCount = (aCount + 2 * bCount);
 
            // If current character is 'c', then
            // there are following possibilities :
            // a) Current character begins a new
            // subsequence of c's with bCount
            // subsequences.
            // b) Current character is part of cCount
            // subsequences.
            // c) Current character is not part of
            // cCount subsequences.
            else if (s[i] == 'c')
                cCount = (bCount + 2 * cCount);
        }
 
        return cCount;
    }
 
    // Driver code
    public static void Main()
    {
        String s = "abbc";
        Console.Write(countSubsequences(s));
    }
}
 
// This code is contributed by Nitin Mittal.


Javascript




<script>
 
// JavaScript program for the above approach
 
    // Returns count of subsequences
    // of the form a^i b^j c^k
    function countSubsequences(s)
    {
        // Initialize counts of different
        // subsequences caused by different
        // combination of 'a'
        let aCount = 0;
   
        // Initialize counts of different
        // subsequences caused by different
        // combination of 'a' and
        // different combination of 'b'
        let bCount = 0;
   
        // Initialize counts of different
        // subsequences caused by different
        // combination of 'a', 'b' and 'c'
        let cCount = 0;
   
        // Traverse all characters of given string
        for (let i = 0; i < s.length; i++) {
   
            // If current character is 'a', then
            // there are following possibilities :
            // a) Current character begins a
            // new subsequence.
            // b) Current character is part
            // of aCount subsequences
            // c) Current character is not part
            // of aCount subsequences.
   
            if (s[i] == 'a')
                aCount = (1 + 2 * aCount);
   
            // If current character is 'b', then
            // there are following possibilities :
            // a) Current character begins a new
            // subsequence of b's with aCount
            // subsequences.
            // b) Current character is part of bCount
            // subsequences.
            // c) Current character is not part of
            // bCount subsequences.
            else if (s[i] == 'b')
                bCount = (aCount + 2 * bCount);
   
            // If current character is 'c', then
            // there are following possibilities :
            // a) Current character begins a new
            // subsequence of c's with bCount
            // subsequences.
            // b) Current character is part of cCount
            // subsequences.
            // c) Current character is not part of
            // cCount subsequences.
            else if (s[i] == 'c')
                cCount = (bCount + 2 * cCount);
        }
   
        return cCount;
    }
// Driver Code
     
     let s = "abbc";
     document.write(countSubsequences(s));
 
</script>


PHP




<?php
// PHP program to count subsequences
// of the form a^i b^j c^k
 
// Returns count of subsequences
// of the form a^i b^j c^k
function countSubsequences($s)
{
     
    // Initialize counts of
    // different subsequences
    // caused by different
    // combination of 'a'
    $aCount = 0;
 
    // Initialize counts of
    // different subsequences
    // caused by different
    // combination of 'a' and
    // different combination of 'b'
    $bCount = 0;
 
    // Initialize counts of
    // different subsequences
    // caused by different
    // combination of 'a', 'b'
    // and 'c'.
    $cCount = 0;
 
    // Traverse all characters
    // of given string
    for($i = 0; $i < strlen($s); $i++)
    {
         
        /* If current character is 'a', then
        there are following possibilities :
            a) Current character begins a new
                subsequence.
            b) Current character is part of aCount
                subsequences.
            c) Current character is not part of
                aCount subsequences. */
        if ($s[$i] == 'a')
            $aCount = (1 + 2 * $aCount);
 
        /* If current character is 'b', then
        there are following possibilities :
            a) Current character begins a new
                subsequence of b's with aCount
                subsequences.
            b) Current character is part of bCount
                subsequences.
            c) Current character is not part of
                bCount subsequences. */
        else if ($s[$i] == 'b')
            $bCount = ($aCount + 2 * $bCount);
 
        /* If current character is 'c', then
        there are following possibilities :
            a) Current character begins a new
                subsequence of c's with bCount
                subsequences.
            b) Current character is part of cCount
                subsequences.
            c) Current character is not part of
                cCount subsequences. */
        else if ($s[$i] == 'c')
            $cCount = ($bCount + 2 * $cCount);
    }
 
    return $cCount;
}
 
    // Driver Code
    $s = "abbc";
    echo countSubsequences($s) ;
 
// This code is contributed by nitin mittal.
?>


Output

3








Complexity Analysis: 

  • Time Complexity: O(n). 
    One traversal of the string is needed.
  • Auxiliary Space: O(1). 
    No extra space is needed.

DP Based Approach:

Base Case: Set dp[0][0][0] = 1 as an empty string is also a valid subsequence.

DP Recurrence and Transition: Iterate through the string character by character. For each character s[idx]:

Update dp[i][j][k] based on the recurrence relation considering the current character:

If s[idx] == ‘a’, update dp[i+1][j][k] += dp[i][j][k] to account for ‘a’.

If s[idx] == ‘b’, update dp[i][j+1][k] += dp[i][j][k] to account for ‘b’.

If s[idx] == ‘c’, update dp[i][j][k+1] += dp[i][j][k] to account for ‘c’.

Final Sub-problem: Finally, the count of subsequences of the form a^i b^j c^k where i >= 1, j >= 1, and k >= 1 will be stored in dp[n][n][n].

C++




#include <bits/stdc++.h>
using namespace std;
 
// Recursive function to count subsequences of the form a^i
// b^j c^k
int countSubsequences(int idx, bool hasA, bool hasB,
                      bool hasC, string& s)
{
    int n = s.size();
 
    // Base case: if the end of string is reached
    if (idx == n) {
        // Check if the required pattern has been formed
        // till now
        if (hasC) {
            return 1; // Valid subsequence found
        }
        return 0; // Invalid subsequence
    }
 
    int opt1 = 0, opt2 = 0;
 
    // Recursive calls based on the current character
    if (hasC) {
        if (s[idx] == 'c') {
            opt1
                = countSubsequences(idx + 1, true, true,
                                    true, s); // Include 'c'
            opt1 += countSubsequences(idx + 1, true, true,
                                      true, s); // Skip 'c'
        }
        else {
            opt2 = countSubsequences(idx + 1, true, true,
                                     true,
                                     s); // Skip 'a' or 'b'
        }
    }
    else if (hasB) {
        if (s[idx] == 'b') {
            opt1 = countSubsequences(idx + 1, true, true,
                                     false,
                                     s); // Include 'b'
            opt1 += countSubsequences(idx + 1, true, true,
                                      false, s); // Skip 'b'
        }
        else if (s[idx] == 'c') {
            opt1
                = countSubsequences(idx + 1, true, true,
                                    true, s); // Include 'c'
            opt1 += countSubsequences(idx + 1, true, true,
                                      false, s); // Skip 'c'
        }
        else {
            opt2 = countSubsequences(idx + 1, true, true,
                                     false, s); // Skip 'a'
        }
    }
    else if (hasA) {
        if (s[idx] == 'a') {
            opt1 = countSubsequences(idx + 1, true, false,
                                     false,
                                     s); // Include 'a'
            opt1 += countSubsequences(idx + 1, true, false,
                                      false, s); // Skip 'a'
        }
        else if (s[idx] == 'b') {
            opt1 = countSubsequences(idx + 1, true, true,
                                     false,
                                     s); // Include 'b'
            opt1 += countSubsequences(idx + 1, true, false,
                                      false, s); // Skip 'b'
        }
        else {
            opt2 = countSubsequences(idx + 1, true, false,
                                     false, s); // Skip 'c'
        }
    }
    else { // No 'a' found yet
        if (s[idx] == 'a') {
            opt1 = countSubsequences(idx + 1, true, false,
                                     false,
                                     s); // Include 'a'
        }
        opt2 = countSubsequences(idx + 1, false, false,
                                 false, s); // Skip 'a'
    }
 
    return opt1 + opt2; // Return total count of valid
                        // subsequences
}
 
int main()
{
    string s = "abbc";
 
    cout << countSubsequences(0, false, false, false, s);
 
    return 0;
}


Java




public class Main {
    // Recursive function to count subsequences of the form a^i b^j c^k
    static int countSubsequences(int idx, boolean hasA, boolean hasB,
                                  boolean hasC, String s) {
        int n = s.length();
 
        // Base case: if the end of string is reached
        if (idx == n) {
            // Check if the required pattern has been formed
            // till now
            if (hasC) {
                return 1; // Valid subsequence found
            }
            return 0; // Invalid subsequence
        }
 
        int opt1 = 0, opt2 = 0;
 
        // Recursive calls based on the current character
        if (hasC) {
            if (s.charAt(idx) == 'c') {
                opt1 = countSubsequences(idx + 1, true, true, true, s); // Include 'c'
                opt1 += countSubsequences(idx + 1, true, true, true, s); // Skip 'c'
            } else {
                opt2 = countSubsequences(idx + 1, true, true, true, s); // Skip 'a' or 'b'
            }
        } else if (hasB) {
            if (s.charAt(idx) == 'b') {
                opt1 = countSubsequences(idx + 1, true, true, false, s); // Include 'b'
                opt1 += countSubsequences(idx + 1, true, true, false, s); // Skip 'b'
            } else if (s.charAt(idx) == 'c') {
                opt1 = countSubsequences(idx + 1, true, true, true, s); // Include 'c'
                opt1 += countSubsequences(idx + 1, true, true, false, s); // Skip 'c'
            } else {
                opt2 = countSubsequences(idx + 1, true, true, false, s); // Skip 'a'
            }
        } else if (hasA) {
            if (s.charAt(idx) == 'a') {
                opt1 = countSubsequences(idx + 1, true, false, false, s); // Include 'a'
                opt1 += countSubsequences(idx + 1, true, false, false, s); // Skip 'a'
            } else if (s.charAt(idx) == 'b') {
                opt1 = countSubsequences(idx + 1, true, true, false, s); // Include 'b'
                opt1 += countSubsequences(idx + 1, true, false, false, s); // Skip 'b'
            } else {
                opt2 = countSubsequences(idx + 1, true, false, false, s); // Skip 'c'
            }
        } else { // No 'a' found yet
            if (s.charAt(idx) == 'a') {
                opt1 = countSubsequences(idx + 1, true, false, false, s); // Include 'a'
            }
            opt2 = countSubsequences(idx + 1, false, false, false, s); // Skip 'a'
        }
 
        return opt1 + opt2; // Return total count of valid subsequences
    }
 
    public static void main(String[] args) {
        String s = "abbc";
        System.out.println(countSubsequences(0, false, false, false, s));
    }
}
 
// This code is contributed by shivamgupta310570


Python




# Recursive function to count subsequences of the form a^i b^j c^k
def count_subsequences(idx, has_a, has_b, has_c, s):
    n = len(s)
 
    # Base case: if the end of the string is reached
    if idx == n:
        # Check if the required pattern has been formed till now
        if has_c:
            return 1  # Valid subsequence found
        return 0  # Invalid subsequence
 
    opt1 = 0
    opt2 = 0
 
    # Recursive calls based on the current character
    if has_c:
        if s[idx] == 'c':
            opt1 = count_subsequences(
                idx + 1, True, True, True, s)  # Include 'c'
            opt1 += count_subsequences(idx + 1, True,
                                       True, True, s)  # Skip 'c'
        else:
            opt2 = count_subsequences(
                idx + 1, True, True, True, s)  # Skip 'a' or 'b'
    elif has_b:
        if s[idx] == 'b':
            opt1 = count_subsequences(
                idx + 1, True, True, False, s)  # Include 'b'
            opt1 += count_subsequences(idx + 1, True,
                                       True, False, s)  # Skip 'b'
        elif s[idx] == 'c':
            opt1 = count_subsequences(
                idx + 1, True, True, True, s)  # Include 'c'
            opt1 += count_subsequences(idx + 1, True,
                                       True, False, s)  # Skip 'c'
        else:
            opt2 = count_subsequences(
                idx + 1, True, True, False, s)  # Skip 'a'
    elif has_a:
        if s[idx] == 'a':
            opt1 = count_subsequences(
                idx + 1, True, False, False, s)  # Include 'a'
            opt1 += count_subsequences(idx + 1, True,
                                       False, False, s)  # Skip 'a'
        elif s[idx] == 'b':
            opt1 = count_subsequences(
                idx + 1, True, True, False, s)  # Include 'b'
            opt1 += count_subsequences(idx + 1, True,
                                       False, False, s)  # Skip 'b'
        else:
            opt2 = count_subsequences(
                idx + 1, True, False, False, s)  # Skip 'c'
    else:
        # No 'a' found yet
        if s[idx] == 'a':
            opt1 = count_subsequences(
                idx + 1, True, False, False, s)  # Include 'a'
        opt2 = count_subsequences(idx + 1, False, False, False, s)  # Skip 'a'
 
    return opt1 + opt2  # Return total count of valid subsequences
 
 
if __name__ == "__main__":
    s = "abbc"
    print(count_subsequences(0, False, False, False, s))


C#




using System;
 
class Program
{
    // Recursive function to count subsequences of the form a^i b^j c^k
    static int CountSubsequences(int idx, bool hasA, bool hasB, bool hasC, string s)
    {
        int n = s.Length;
 
        // Base case: if the end of string is reached
        if (idx == n)
        {
            // Check if the required pattern has been formed till now
            if (hasC)
            {
                return 1; // Valid subsequence found
            }
            return 0; // Invalid subsequence
        }
 
        int opt1 = 0, opt2 = 0;
 
        // Recursive calls based on the current character
        if (hasC)
        {
            if (s[idx] == 'c')
            {
                opt1 = CountSubsequences(idx + 1, true, true, true, s); // Include 'c'
                opt1 += CountSubsequences(idx + 1, true, true, true, s); // Skip 'c'
            }
            else
            {
                opt2 = CountSubsequences(idx + 1, true, true, true, s); // Skip 'a' or 'b'
            }
        }
        else if (hasB)
        {
            if (s[idx] == 'b')
            {
                opt1 = CountSubsequences(idx + 1, true, true, false, s); // Include 'b'
                opt1 += CountSubsequences(idx + 1, true, true, false, s); // Skip 'b'
            }
            else if (s[idx] == 'c')
            {
                opt1 = CountSubsequences(idx + 1, true, true, true, s); // Include 'c'
                opt1 += CountSubsequences(idx + 1, true, true, false, s); // Skip 'c'
            }
            else
            {
                opt2 = CountSubsequences(idx + 1, true, true, false, s); // Skip 'a'
            }
        }
        else if (hasA)
        {
            if (s[idx] == 'a')
            {
                opt1 = CountSubsequences(idx + 1, true, false, false, s); // Include 'a'
                opt1 += CountSubsequences(idx + 1, true, false, false, s); // Skip 'a'
            }
            else if (s[idx] == 'b')
            {
                opt1 = CountSubsequences(idx + 1, true, true, false, s); // Include 'b'
                opt1 += CountSubsequences(idx + 1, true, false, false, s); // Skip 'b'
            }
            else
            {
                opt2 = CountSubsequences(idx + 1, true, false, false, s); // Skip 'c'
            }
        }
        else // No 'a' found yet
        {
            if (s[idx] == 'a')
            {
                opt1 = CountSubsequences(idx + 1, true, false, false, s); // Include 'a'
            }
            opt2 = CountSubsequences(idx + 1, false, false, false, s); // Skip 'a'
        }
 
        return opt1 + opt2; // Return total count of valid subsequences
    }
 
    static void Main()
    {
        string s = "abbc";
 
        Console.WriteLine(CountSubsequences(0, false, false, false, s));
 
    }
}


Javascript




// Recursive function to count subsequences of the form a^i b^j c^k
function countSubsequences(idx, hasA, hasB, hasC, s) {
    const n = s.length;
 
    // Base case: if the end of string is reached
    if (idx === n) {
        // Check if the required pattern has been formed till now
        if (hasC) {
            return 1; // Valid subsequence found
        }
        return 0; // Invalid subsequence
    }
 
    let opt1 = 0, opt2 = 0;
 
    // Recursive calls based on the current character
    if (hasC) {
        if (s[idx] === 'c') {
            opt1 = countSubsequences(idx + 1, true, true, true, s); // Include 'c'
            opt1 += countSubsequences(idx + 1, true, true, true, s); // Skip 'c'
        } else {
            opt2 = countSubsequences(idx + 1, true, true, true, s); // Skip 'a' or 'b'
        }
    } else if (hasB) {
        if (s[idx] === 'b') {
            opt1 = countSubsequences(idx + 1, true, true, false, s); // Include 'b'
            opt1 += countSubsequences(idx + 1, true, true, false, s); // Skip 'b'
        } else if (s[idx] === 'c') {
            opt1 = countSubsequences(idx + 1, true, true, true, s); // Include 'c'
            opt1 += countSubsequences(idx + 1, true, true, false, s); // Skip 'c'
        } else {
            opt2 = countSubsequences(idx + 1, true, true, false, s); // Skip 'a'
        }
    } else if (hasA) {
        if (s[idx] === 'a') {
            opt1 = countSubsequences(idx + 1, true, false, false, s); // Include 'a'
            opt1 += countSubsequences(idx + 1, true, false, false, s); // Skip 'a'
        } else if (s[idx] === 'b') {
            opt1 = countSubsequences(idx + 1, true, true, false, s); // Include 'b'
            opt1 += countSubsequences(idx + 1, true, false, false, s); // Skip 'b'
        } else {
            opt2 = countSubsequences(idx + 1, true, false, false, s); // Skip 'c'
        }
    } else { // No 'a' found yet
        if (s[idx] === 'a') {
            opt1 = countSubsequences(idx + 1, true, false, false, s); // Include 'a'
        }
        opt2 = countSubsequences(idx + 1, false, false, false, s); // Skip 'a'
    }
 
    return opt1 + opt2; // Return total count of valid subsequences
}
 
const s = "abbc";
console.log(countSubsequences(0, false, false, false, s));


Complexity Analysis: 

Time Complexity: O(n). 

Auxiliary Space: O(n). 

This article is contributed by Mr. Somesh Awasthi.  



Last Updated : 03 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads