# Count of subsequences of length 4 in form (x, x, x+1, x+1) | Set 2

Given a large number in form of string str of size N, the task is to count the subsequence of length 4 whose digit are in the form of (x, x, x+1, x+1).

Example:

Input: str = “1515732322”
Output: 3
Explanation:
For the given input string str = “1515732322”, there are 3 subsequence {1122}, {1122}, and {1122} which are in the given form of (x, x, x+1, x+1).

Input: str = “224353”
Output: 1
Explanation:
For the given input string str = “224353”, there is only 1 subsequence possible {2233} in the given form of (x, x, x+1, x+1).

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Prefix Sum Approach: Please refer to the Set 1, for the Prefix sum approach.

Dynamic Programming Approach: This problem can be solved using Dynamic Programming.

We will be using 2 arrays as count1[][j] and count2[] such that count1[i] will store the count of consecutive equal element of digit j at current index i traversing string from left and count2[i][j] will store the count of consecutive equal element of digit j at current index i traversing string from right. Below are the steps:

1. Initialize two count array count1[] for filling table from left to right and count2[] for filling table from right to left of input string.
2. Traverse input string and fill the both count1 and count2 array.
3. Recurrence Relation for count1[][] is given by:

count1[i][j] += count1[i – 1][j]
where count1[i][j] is the count of two adjacent at index i for digit j

4. Recurrence Relation for count2[][] is given by:

count2[i][j] += count1[i + 1][j]
where count2[i][j] is the count of two adjacent at index i for digit j

5. Initialize a variable ans to 0 that stores the resultant count of stable numbers.
6. Traverse the input string and get the count of numbers from count1[][] and count2[][] array such that difference between number from count1[][] and count2[][] array is 1 and store it in variable c1 and c2.
7. Finally update result(say ans) with (c1 * ((c2 * (c2 – 1) / 2))).
8. Print the answer ans calculated above.

Below is the implementation of above approach:

 `// C++ program for the above approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to count the numbers ` `int` `countStableNum(string str, ``int` `N) ` `{ ` `    ``// Array that stores the ` `    ``// digits from left to right ` `    ``int` `count1[N]; ` ` `  `    ``// Array that stores the ` `    ``// digits from right to left ` `    ``int` `count2[N]; ` ` `  `    ``// Initially both array store zero ` `    ``for` `(``int` `i = 0; i < N; i++) ` `        ``for` `(``int` `j = 0; j < 10; j++) ` `            ``count1[i][j] = count2[i][j] = 0; ` ` `  `    ``// Fill the table for count1 array ` `    ``for` `(``int` `i = 0; i < N; i++) { ` `        ``if` `(i != 0) { ` `            ``for` `(``int` `j = 0; j < 10; j++) { ` `                ``count1[i][j] += count1[i - 1][j]; ` `            ``} ` `        ``} ` ` `  `        ``// Update the count of current character ` `        ``count1[i][str[i] - ``'0'``]++; ` `    ``} ` ` `  `    ``// Fill the table for count2 array ` `    ``for` `(``int` `i = N - 1; i >= 0; i--) { ` `        ``if` `(i != N - 1) { ` `            ``for` `(``int` `j = 0; j < 10; j++) { ` `                ``count2[i][j] += count2[i + 1][j]; ` `            ``} ` `        ``} ` ` `  `        ``// Update the count of cuuent character ` `        ``count2[i][str[i] - ``'0'``]++; ` `    ``} ` ` `  `    ``// Variable that stores the ` `    ``// count of the numbers ` `    ``int` `ans = 0; ` ` `  `    ``// Traverse Input string and get the ` `    ``// count of digits from count1 and ` `    ``// count2 array such that difference ` `    ``// b/w digit is 1 & store it int c1 &c2. ` `    ``// And store it in variable c1 and c2 ` `    ``for` `(``int` `i = 1; i < N - 1; i++) { ` ` `  `        ``if` `(str[i] == ``'9'``) ` `            ``continue``; ` ` `  `        ``int` `c1 = count1[i - 1][str[i] - ``'0'``]; ` `        ``int` `c2 = count2[i + 1][str[i] - ``'0'` `+ 1]; ` ` `  `        ``if` `(c2 == 0) ` `            ``continue``; ` ` `  `        ``// Update the ans ` `        ``ans = (ans ` `               ``+ (c1 * ((c2 * (c2 - 1) / 2)))); ` `    ``} ` ` `  `    ``// Return the final count ` `    ``return` `ans; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``// Given String ` `    ``string str = ``"224353"``; ` `    ``int` `N = str.length(); ` ` `  `    ``// Function Call ` `    ``cout << countStableNum(str, N); ` ` `  `    ``return` `0; ` `} `

Output:

```1
```

Time Complexty: O(N)
Auxiliary Space Complexty: O(N) My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.