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:4Explanation:

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 formed` `void` `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 Code` `int` `main()` `{` ` ` `string S = ` `"0111022110"` `;` ` ` `differentStrings(S);` ` ` `return` `0;` `}` |

## Java

`// Java implementation of the above approach` `import` `java.io.*;` `class` `GFG{` `// Function that prints the` `// number of different strings` `// that can be formed` `static` `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 code` `public` `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 formed` `def` `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 Code` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `S ` `=` `"0111022110"` ` ` `differentStrings(S)` ` ` `# This code is contributed by Chitranayal` |

## C#

`// C# implementation of the above approach` `using` `System;` `class` `GFG{` `// Function that prints the` `// number of different strings` `// that can be formed` `static` `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 code` `public` `static` `void` `Main()` `{` ` ` `string` `S = ` `"0111022110"` `;` ` ` `differentStrings(S);` `}` `}` `// This code is contributed by Code_Mech` |

**Output:**

12

**Time Complexity:** *O(N)*

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. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price.

In case you wish to attend live classes with industry experts, please refer **Geeks Classes Live** and **Geeks Classes Live USA**