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.
Examples:
Input: S = “0110000022”
Output: 4
Explanation:
There can be four different formed by using the operations, the four strings are
“0110000022”, “030000022”, “03000004”, “011000004”
Input: S = “111”
Output: 3
Approach:
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[0] = dp[1] = 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:
C++
// C++ implementation of// the above approach#include using namespace std;// Function that prints the// number of different strings// that can be formedvoid differentStrings(string s){ // Computing the length of // the given string int n = s.length(); vector dp(n + 1); // Base case dp[0] = dp[1] = 1; // Traverse the given string for (int i = 1; i < n; i++) { // If two consecutive 1's // or 2's are present if (s[i] == s[i - 1] && (s[i] == '1' || s[i] == '2')) dp[i + 1] = dp[i] + dp[i - 1]; // Otherwise take // the previous value else dp[i + 1] = dp[i]; } cout << dp[n] << "\n";}// Driver Codeint main(){ string S = "0111022110"; differentStrings(S); return 0;} |
Java
// Java implementation of the above approachimport java.io.*;class GFG{// Function that prints the// number of different strings// that can be formedstatic void differentStrings(String s){ // Computing the length of // the given string int n = s.length(); int[] dp = new int[n + 1]; // Base case dp[0] = dp[1] = 1; // Traverse the given string for(int i = 1; i < n; i++) { // If two consecutive 1's // or 2's are present if (s.charAt(i) == s.charAt(i - 1) && (s.charAt(i) == '1' || s.charAt(i) == '2')) dp[i + 1] = dp[i] + dp[i - 1]; // Otherwise take the // previous value else dp[i + 1] = dp[i]; } System.out.println(dp[n]);}// Driver codepublic static void main(String[] args){ String S = "0111022110"; differentStrings(S);}}// This code is contributed by offbeat |
Python3
# Python3 implementation of# the above approach# Function that prints the# number of different strings# that can be formeddef differentStrings(s): # Computing the length of # the given string n = len(s) dp = [0] * (n + 1) # Base case dp[0] = dp[1] = 1 # Traverse the given string for i in range (1, n): # If two consecutive 1's # or 2's are present if (s[i] == s[i - 1] and (s[i] == '1' or s[i] == '2')): dp[i + 1] = dp[i] + dp[i - 1] # Otherwise take # the previous value else: dp[i + 1] = dp[i] print (dp[n])# Driver Codeif __name__ == "__main__": S = "0111022110" differentStrings(S) # This code is contributed by Chitranayal |
C#
// C# implementation of the above approachusing System;class GFG{// Function that prints the// number of different strings// that can be formedstatic void differentStrings(string s){ // Computing the length of // the given string int n = s.Length; int[] dp = new int[n + 1]; // Base case dp[0] = dp[1] = 1; // Traverse the given string for(int i = 1; i < n; i++) { // If two consecutive 1's // or 2's are present if (s[i] == s[i - 1] && (s[i] == '1' || s[i] == '2')) dp[i + 1] = dp[i] + dp[i - 1]; // Otherwise take the // previous value else dp[i + 1] = dp[i]; } Console.Write(dp[n]);}// Driver codepublic static void Main(){ string S = "0111022110"; differentStrings(S);}}// This code is contributed by Code_Mech |
12
Time Complexity: O(N)
Recommended Posts:
- 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 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.


