Given a numeric string S consisting of only three types of characters 0, 1, and 2 initially and following two operations:
- The occurrence of two consecutive 1 can be replaced by 3.
- The occurrence of two consecutive 2 can be replaced by 4.
The given task is to find the total number of different strings that can be formed by using the operations.
Input: S = “0110000022”
There can be four different formed by using the operations, the four strings are
“0110000022”, “030000022”, “03000004”, “011000004”
Input: S = “111”
In order to solve this problem, we are using a dynamic programming approach. We define an array dp to store the count of possible strings of length equal to its respective indices.
- Initialize dp = dp = 1.
- For any index i between [1, N-1], if the character at that index is ‘1’ or ‘2’ and is equal to that of its previous index, add the possible strings that can be formed of length i-1 and i-2. Otherwise, it is the same as that of length i-1.
dp[i + 1] = dp[i] + dp[i-1] if S[i] == S[i-1] where S[i] is either ‘1’ or ‘2’.
dp[i + 1] = dp[i] otherwise.
- Return dp[n] as the count of different strings possible.
Below is the implementation of the above approach:
Time Complexity: O(N)
- Maximum Possible Product in Array after performing given Operations
- Maximum score possible after performing given operations on an Array
- Maximum count of equal numbers in an array after performing given operations
- Maximum count of values of S modulo M lying in a range [L, R] after performing given operations on the array
- Count of distinct Strings possible by swapping prefixes of pairs of Strings from the Array
- Final string after performing given operations
- Maximum sum of all elements of array after performing given operations
- Minimum N-Digit number required to obtain largest N-digit number after performing given operations
- Find the maximum occurring character after performing the given operations
- Maximum possible array sum after performing the given operation
- Number of array elements derivable from D after performing certain operations
- Count of Distinct strings possible by inserting K characters in the original string
- Count of operations to make all elements of array a equal to its min element by performing a[i] – b[i]
- Maximum value obtained by performing given operations in an Array
- Distinct strings such that they contains given strings as sub-sequences
- Count of strings that become equal to one of the two strings after one removal
- Sub-string that contains all lowercase alphabets after performing the given operation
- Find the final number obtained after performing the given operation
- Count of distinct permutations of every possible length of given string
- Minimum steps to reach end from start by performing multiplication and mod operations with array elements
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.