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

Given a string, count number of subsequences of the form a^{i}b^{j}c^{k}, 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

We traverse given string. For every character encounter, we do 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 :

a) Current character begins a new subsequence.

b) Current character is part of aCount subsequences.

c) 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 :

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.

Therefore we do 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.

Therefore we do cCount = (bCount + 2 * cCount);

**5)** Finally we return cCount;

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 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; ` `}` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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. ` ` ` `elif` `(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. ` ` ` `elif` `(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 ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## 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. ` `?> ` |

*chevron_right*

*filter_none*

Output:

3

Time Complexity : O(n)

This article is contributed by **Mr. Somesh Awasthi**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9 | Set 2
- Sum of all subsequences of a number
- Number of subsequences with zero sum
- Minimum number of increasing subsequences
- Number of subsequences in a string divisible by n
- Number of palindromic subsequences of length k where k <= 3
- Number of subsequences as "ab" in a string repeated K times
- Number of GP (Geometric Progression) subsequences of size 3
- Count number of increasing subsequences of size k
- Number of ways to partition a string into two balanced subsequences
- Count minimum number of subsets (or subsequences) with consecutive numbers
- Minimum number of palindromic subsequences to be removed to empty a binary string
- Form minimum number from given sequence
- Form the smallest number using at most one swap operation