Count of distinct possible strings after performing given operations
Last Updated :
20 Oct, 2023
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++
#include
using namespace std;
void differentStrings(string s)
{
int n = s.length();
vector dp(n + 1);
dp[0] = dp[1] = 1;
for ( int i = 1; i < n; i++) {
if (s[i] == s[i - 1]
&& (s[i] == '1'
|| s[i] == '2' ))
dp[i + 1]
= dp[i] + dp[i - 1];
else
dp[i + 1] = dp[i];
}
cout << dp[n] << "\n" ;
}
int main()
{
string S = "0111022110" ;
differentStrings(S);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void differentStrings( char * s)
{
int n = strlen (s);
int dp[n + 1];
dp[0] = dp[1] = 1;
for ( int i = 1; i < n; i++) {
if (s[i] == s[i - 1]
&& (s[i] == '1'
|| s[i] == '2' ))
dp[i + 1] = dp[i] + dp[i - 1];
else
dp[i + 1] = dp[i];
}
printf ( "%d\n" , dp[n]);
}
int main()
{
char S[] = "0111022110" ;
differentStrings(S);
return 0;
}
|
Java
import java.io.*;
class GFG{
static void differentStrings(String s)
{
int n = s.length();
int [] dp = new int [n + 1 ];
dp[ 0 ] = dp[ 1 ] = 1 ;
for ( int i = 1 ; i < n; i++)
{
if (s.charAt(i) == s.charAt(i - 1 ) &&
(s.charAt(i) == '1' ||
s.charAt(i) == '2' ))
dp[i + 1 ] = dp[i] + dp[i - 1 ];
else
dp[i + 1 ] = dp[i];
}
System.out.println(dp[n]);
}
public static void main(String[] args)
{
String S = "0111022110" ;
differentStrings(S);
}
}
|
Python3
def differentStrings(s):
n = len (s)
dp = [ 0 ] * (n + 1 )
dp[ 0 ] = dp[ 1 ] = 1
for i in range ( 1 , n):
if (s[i] = = s[i - 1 ] and
(s[i] = = '1' or
s[i] = = '2' )):
dp[i + 1 ] = dp[i] + dp[i - 1 ]
else :
dp[i + 1 ] = dp[i]
print (dp[n])
if __name__ = = "__main__" :
S = "0111022110"
differentStrings(S)
|
C#
using System;
class GFG{
static void differentStrings( string s)
{
int n = s.Length;
int [] dp = new int [n + 1];
dp[0] = dp[1] = 1;
for ( int i = 1; i < n; i++)
{
if (s[i] == s[i - 1] &&
(s[i] == '1' ||
s[i] == '2' ))
dp[i + 1] = dp[i] + dp[i - 1];
else
dp[i + 1] = dp[i];
}
Console.Write(dp[n]);
}
public static void Main()
{
string S = "0111022110" ;
differentStrings(S);
}
}
|
Javascript
<script>
function differentStrings(s)
{
var n = s.length;
var dp = Array(n + 1);
dp[0] = dp[1] = 1;
for ( var i = 1; i < n; i++) {
if (s[i] == s[i - 1]
&& (s[i] == '1'
|| s[i] == '2' ))
dp[i + 1]
= dp[i] + dp[i - 1];
else
dp[i + 1] = dp[i];
}
document.write( dp[n] + "<br>" );
}
var S = "0111022110" ;
differentStrings(S);
</script>
|
Time Complexity: O(N), as we are using a loop to traverse N times so it will cost us O(N) time
Auxiliary Space: O(N), as we are using extra space for DP array.
Efficient approach : Space optimization O(1)
In previous approach we the current value dp[i] is only depend upon the previous 2 values i.e. dp[i-1] and dp[i+1]. So to optimize the space we can keep track of previous and next values by the help of three variables prev and next which will reduce the space complexity from O(N) to O(1).
Implementation Steps:
- Create 2 variables prev1 and prev2 to keep track o previous values of DP.
- Initialize base case prev = curr = 1.
- Create a variable curr to store current value.
- Iterate over subproblem using loop and update curr.
- Create variable next to keep track of next value of DP.
- After every iteration update prev and curr for further iterations.
- At last return curr.
Implementation:
C++
#include <iostream>
using namespace std;
void differentStrings(string s)
{
int n = s.length();
int prev = 1, curr = 1;
for ( int i = 1; i < n; i++) {
if (s[i] == s[i - 1]
&& (s[i] == '1'
|| s[i] == '2' )) {
int next = prev + curr;
prev = curr;
curr = next;
}
else {
prev = curr;
}
}
cout << curr << "\n" ;
}
int main()
{
string S = "0111022110" ;
differentStrings(S);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void differentStrings(String s)
{
int n = s.length();
int prev = 1 , curr = 1 ;
for ( int i = 1 ; i < n; i++) {
if (s.charAt(i) == s.charAt(i - 1 )
&& (s.charAt(i) == '1' || s.charAt(i) == '2' )) {
int next = prev + curr;
prev = curr;
curr = next;
}
else {
prev = curr;
}
}
System.out.println(curr);
}
public static void main(String[] args) {
String S = "0111022110" ;
differentStrings(S);
}
}
|
Python3
def different_strings(s: str ) - > None :
n = len (s)
prev, curr = 1 , 1
for i in range ( 1 , n):
if s[i] = = s[i - 1 ] and (s[i] = = '1' or s[i] = = '2' ):
next = prev + curr
prev = curr
curr = next
else :
prev = curr
print (curr)
if __name__ = = '__main__' :
S = "0111022110"
different_strings(S)
|
C#
using System;
public class Program
{
public static void DifferentStrings( string s)
{
int n = s.Length;
int prev = 1, curr = 1;
for ( int i = 1; i < n; i++)
{
if (s[i] == s[i - 1] && (s[i] == '1' || s[i] == '2' ))
{
int next = prev + curr;
prev = curr;
curr = next;
}
else
{
prev = curr;
}
}
Console.WriteLine(curr);
}
public static void Main( string [] args)
{
string S = "0111022110" ;
DifferentStrings(S);
}
}
|
Javascript
function differentStrings(s) {
const n = s.length;
let prev = 1, curr = 1;
for (let i = 1; i < n; i++) {
if (s[i] === s[i - 1] && (s[i] === '1' || s[i] === '2' )) {
const next = prev + curr;
prev = curr;
curr = next;
} else {
prev = curr;
}
}
console.log(curr);
}
const S = "0111022110" ;
differentStrings(S);
|
Output:
12
Time Complexity: O(N), as we are using a loop to traverse N times so it will cost us O(N) time
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...