Count of strings possible by replacing two consecutive same character with new character
Last Updated :
04 Jan, 2023
Given string str. The task is to count the number of all different strings possible if two consecutive same characters of the string can be replaced by one different character.
Examples
Input: str = “abclll”
Output: 3
Explanation:
There can be 3 different string including the original string as shown in the below figure:-
Input: str = “abcllldefkkkk”
Output: 15
Explanation:
There can be 15 different string including the original string as shown in the below figure:-
Approach:
The following properties were observed for replacing two equal characters at a time:
- If for string = “aaa” of length 3 we replace two “aa” with one character say “K” then the total number of different possible strings including the original string is:- Ka, aK, aaa. Therefore the number of different strings follows the property of Fibonacci Number of the length of the consecutive characters in the string.
- If string = “aaadefyyyy” then the total number of possible different strings is equaled to the product of combinations of string “aaa” and “yyyy” by replacing the two consecutive characters at a time.
Hence from the above two observations, the count of different possible strings with N consecutive characters is given by N-th Fibonacci Number. Therefore, the total number of different possible strings for the given string str is equal to the product of count of different possible strings for every substring with all same characters.
The following are the steps:
- Count(say cnt) the number of consecutive characters which are same in the given string str.
- To count the different possible strings for the count cnt, find the value of Fibonacci Sequence at cnt.
- Repeat the above steps for all the consecutive characters in the given string str.
- The total count of different possible strings is equaled to the product of all the value of Fibonacci Sequence obtained for every count of consecutive characters.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int fib[100005];
void computeFibonacci()
{
fib[0] = 1;
fib[1] = 1;
for ( int i = 2; i < 100005; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
}
int countString(string str)
{
int ans = 1;
int cnt = 1;
for ( int i = 1; str[i]; i++) {
if (str[i] == str[i - 1]) {
cnt++;
}
else {
ans = ans * fib[cnt];
cnt = 1;
}
}
ans = ans * fib[cnt];
return ans;
}
int main()
{
string str = "abdllldefkkkk" ;
computeFibonacci();
cout << countString(str);
return 0;
}
|
Java
class GFG {
static int fib[] = new int [ 100005 ];
static void computeFibonacci()
{
fib[ 0 ] = 1 ;
fib[ 1 ] = 1 ;
for ( int i = 2 ; i < 100005 ; i++) {
fib[i] = fib[i - 1 ] + fib[i - 2 ];
}
}
static int countString(String str)
{
int ans = 1 ;
int cnt = 1 ;
for ( int i = 1 ; i<str.length(); i++) {
if (str.charAt(i) == str.charAt(i - 1 )) {
cnt++;
}
else {
ans = ans * fib[cnt];
cnt = 1 ;
}
}
ans = ans * fib[cnt];
return ans;
}
public static void main (String[] args)
{
String str = "abdllldefkkkk" ;
computeFibonacci();
System.out.println(countString(str));
}
}
|
Python3
fib = [ 0 ] * 100005 ;
def computeFibonacci() :
fib[ 0 ] = 1 ;
fib[ 1 ] = 1 ;
for i in range ( 2 , 100005 ) :
fib[i] = fib[i - 1 ] + fib[i - 2 ];
def countString(string) :
ans = 1 ;
cnt = 1 ;
for i in range ( 1 , len (string)) :
if (string[i] = = string[i - 1 ]) :
cnt + = 1 ;
else :
ans = ans * fib[cnt];
cnt = 1 ;
ans = ans * fib[cnt];
return ans;
if __name__ = = "__main__" :
string = "abdllldefkkkk" ;
computeFibonacci();
print (countString(string));
|
C#
using System;
class GFG {
static int []fib = new int [100005];
static void computeFibonacci()
{
fib[0] = 1;
fib[1] = 1;
for ( int i = 2; i < 100005; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
}
static int countString( string str)
{
int ans = 1;
int cnt = 1;
for ( int i = 1; i < str.Length; i++) {
if (str[i] == str[i - 1]) {
cnt++;
}
else {
ans = ans * fib[cnt];
cnt = 1;
}
}
ans = ans * fib[cnt];
return ans;
}
public static void Main ( string [] args)
{
string str = "abdllldefkkkk" ;
computeFibonacci();
Console.WriteLine(countString(str));
}
}
|
Javascript
<script>
fib = Array(100005).fill(0);
function computeFibonacci()
{
fib[0] = 1;
fib[1] = 1;
for (i = 2; i < 100005; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
}
function countString( str) {
var ans = 1;
var cnt = 1;
for (i = 1; i < str.length; i++)
{
if (str.charAt(i) == str.charAt(i - 1))
{
cnt++;
}
else {
ans = ans * fib[cnt];
cnt = 1;
}
}
ans = ans * fib[cnt];
return ans;
}
var str = "abdllldefkkkk" ;
computeFibonacci();
document.write(countString(str));
</script>
|
Time Complexity: O(N), where N is the length of the given string.
Auxiliary Space: O(M), where M = 100005
Share your thoughts in the comments
Please Login to comment...