Given a string str consisting of digits, the task is to find the number of possible 4 digit sub-sequences which are of the form (x, x, x + 1, x + 1) where x can be from the range [0, 8].
Input: str = “1122”
Only one sub-sequence is valid, i.e the entire string itself.
Input: str = “13134422”
Two Valid sub-sequences are present “1122” and “3344”.
- We will find out total number of possible sub-sequences for each possible x from 0 to 8.
- For each x, remove all other digits from the String, except x and x+1 as they do not affect the answer.
- Maintain a prefix Sum array to count the number of x+1 digits till i th index in the String.
- Now, for every club of digits say size K (which are x), we can choose two numbers in KC2 ways. Last two numbers can be any two numbers from all the digits (which are x+1) which follows that club of digits (count is determined using Prefix Sum Array) say size L, so there are LC2 ways to choose. Total Ways = KC2 * LC2 .
- Till, Now we can considered x to come from same club, but it can also be from multiple Clubs. So, we have to consider all possible pairs of clubs and multiply their size to get number of ways to choose first two numbers. For last two numbers ways will remain same.
- In order to prevent problem of over counting in Step 5. Only Possible way which include current club under consideration will be choosen as other have already been considered in calculation of previous clubs.
- Add all the ways possible for all the values of x and take Modulo.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count index pairs which satisfy the given condition
- Count of indices in an array that satisfy the given condition
- Count all possible N digit numbers that satisfy the given condition
- Count triplet pairs (A, B, C) of points in 2-D space that satisfy the given condition
- Number of strings that satisfy the given condition
- Pairs from an array that satisfy the given condition
- Append two elements to make the array satisfy the given condition
- Find the Number of Permutations that satisfy the given condition in an array
- Minimum operations required to make the string satisfy the given condition
- Mimimum number of leaves required to be removed from a Tree to satisfy the given condition
- Count pairs of strings that satisfy the given conditions
- Count of Fibonacci pairs which satisfy the given equation
- Count of N digit numbers possible which satisfy the given conditions
- Count of unordered pairs (x, y) of Array which satisfy given equation
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x + y*y < n
- Count of 'GFG' Subsequences in the given string
- Count all increasing subsequences
- Count Distinct Subsequences
- Count all subsequences having product less than K
- Count of pairs satisfying the given condition
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.