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).

**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[][10]** such that **count1[i][10]** 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:

- Initialize two count array
**count1[][10]**for filling table from left to right and**count2[][10]**for filling table from right to left of input string. - Traverse input string and fill the both count1 and count2 array.
- 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 - 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 - Initialize a variable
**ans to 0**that stores the resultant count of stable numbers. - 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.** - Finally update result(say
**ans**) with**(c1 * ((c2 * (c2 – 1) / 2)))**. - Print the answer
**ans**calculated above.

Below is the implementation of above approach:

`// C++ program for the above approach ` `#include <bits/stdc++.h> ` `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][10]; ` ` ` ` ` `// Array that stores the ` ` ` `// digits from right to left ` ` ` `int` `count2[N][10]; ` ` ` ` ` `// 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; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

1

**Time Complexty:** *O(N)*

Auxiliary Space Complexty: O(N)

## Recommended Posts:

- Count unique subsequences of length K
- Count the number of subsequences of length k having equal LCM and HCF
- Count of possible subarrays and subsequences using given length of Array
- Count of subsequences of length atmost K containing distinct prime elements
- Number of subsequences of the form a^i b^j c^k
- Sum of all subsequences of length K
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Unique subsequences of length K with given sum
- Number of K length subsequences with minimum sum
- Number of palindromic subsequences of length k where k <= 3
- Minimize sum of smallest elements from K subsequences of length L
- Find two equal subsequences of maximum length with at least one different index
- Number of subsequences of maximum length K containing no repeated elements
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M
- Count Distinct Subsequences
- Count all subsequences having product less than K
- Count of 'GFG' Subsequences in the given string
- Count all increasing subsequences
- Count of subsequences consisting of the same element

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.