Count the number of Special Strings of a given length N
Given the length N of the string, we have to find the number of special strings of length N.
A string is called a special string if it consists only of lowercase letters a and b and there is at least one b between two a’s in the string. Since the number of strings may be very large, therefore print it modulo 10^9+7.
Examples:
Input: N = 2
Output: 3
Explanation :
The number of special string so length 2 are 3 i.e. “ab”, “ba”, “bb”
Input: N = 3
Output: 5
Explanation:
The number of special string so length 3 are 5 i.e. “abb”, “aba”, “bab”, “bba”, “bbb”
Approach:
To solve the problem mentioned above, the first observation is if the integer N is 0 then there can only be an empty string as the answer, if N is 1 then there can be two string “a” or “b” as an answer but if the value of N is greater than 1 then the answer is equal to the sum of previous two terms. Now to find the count of special strings we run a loop and for each integer i count of the special string of length i is equal to the sum of the count of special strings of length i-1 and count of special strings of length i-2. Store the value of each integer in an array and return the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
long count_special( long n)
{
long fib[n + 1];
fib[0] = 1;
fib[1] = 2;
for ( int i = 2; i <= n; i++) {
fib[i] = (fib[i - 1] % mod + fib[i - 2] % mod) % mod;
}
return fib[n];
}
int main()
{
long n = 3;
cout << count_special(n) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static final int mod = 1000000007 ;
static int count_special( int n)
{
int []fib = new int [n + 1 ];
fib[ 0 ] = 1 ;
fib[ 1 ] = 2 ;
for ( int i = 2 ; i <= n; i++)
{
fib[i] = (fib[i - 1 ] % mod +
fib[i - 2 ] % mod) % mod;
}
return fib[n];
}
public static void main(String[] args)
{
int n = 3 ;
System.out.print(count_special(n) + "\n" );
}
}
|
Python3
mod = 1000000007
def count_special(n):
fib = [ 0 for i in range (n + 1 )]
fib[ 0 ] = 1
fib[ 1 ] = 2
for i in range ( 2 , n + 1 , 1 ):
fib[i] = (fib[i - 1 ] % mod +
fib[i - 2 ] % mod) % mod
return fib[n]
if __name__ = = '__main__' :
n = 3
print (count_special(n))
|
C#
using System;
class GFG{
const int mod = 1000000007;
static int count_special( int n)
{
int []fib = new int [n + 1];
fib[0] = 1;
fib[1] = 2;
for ( int i = 2; i <= n; i++)
{
fib[i] = (fib[i - 1] % mod +
fib[i - 2] % mod) % mod;
}
return fib[n];
}
public static void Main()
{
int n = 3;
Console.Write(count_special(n) + "\n" );
}
}
|
Javascript
<script>
var mod = 1000000007;
function count_special(n) {
var fib = [...Array(n + 1)];
fib[0] = 1;
fib[1] = 2;
for ( var i = 2; i <= n; i++) {
fib[i] = ((fib[i - 1] % mod) + (fib[i - 2] % mod)) % mod;
}
return fib[n];
}
var n = 3;
document.write(count_special(n) + "<br>" );
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Efficient Approach: Space optimization using variables.
In previous approach the fib[i] is depend upon fib[i-1] and fib[i-2] So we can optimize space by storing these 2 values in form of variables where prev1 is fib[i-1] and prev2 is determine fib[i-2]
Implementation Steps:
- Take variable prev1 and prev2 and initialize then with base cases.
- Take another variable curr determine the current computation.
- Iterate over every subproblems and calculate curr from prev1 and prev2.
- After every iteration update prev1 and prev2 for further computation.
- At last return the answer stored in curr.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
long count_special( long n)
{
int curr;
int prev1 =1;
int prev2= 2 ;
for ( int i = 2; i <= n; i++) {
curr = (prev1 % mod + prev2 % mod) % mod;
prev1 = prev2;
prev2 = curr;
}
return curr;
}
int main()
{
long n = 2;
cout << count_special(n) << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
static final long mod = 1000000007 ;
static long count_special( long n) {
long curr = 0 ;
long prev1 = 1 ;
long prev2 = 2 ;
for ( long i = 2 ; i <= n; i++) {
curr = (prev1 % mod + prev2 % mod) % mod;
prev1 = prev2;
prev2 = curr;
}
return curr;
}
public static void main(String[] args) {
long n = 2 ;
System.out.println(count_special(n));
}
}
|
Python3
mod = 1000000007
def count_special(n):
curr = 0
prev1 = 1
prev2 = 2
for i in range ( 2 , n + 1 ):
curr = (prev1 % mod + prev2 % mod) % mod
prev1 = prev2
prev2 = curr
return curr
n = 2
print (count_special(n))
|
C#
using System;
public class GFG
{
const int Mod = 1000000007;
static long CountSpecial( long n)
{
int curr = 0;
int prev1 = 1;
int prev2 = 2;
for ( int i = 2; i <= n; i++)
{
curr = (prev1 % Mod + prev2 % Mod) % Mod;
prev1 = prev2;
prev2 = curr;
}
return curr;
}
public static void Main()
{
long n = 2;
Console.WriteLine(CountSpecial(n));
}
}
|
Javascript
const mod = 1000000007;
function count_special(n) {
let curr;
let prev1 = 1;
let prev2 = 2;
for (let i = 2; i <= n; i++) {
curr = (prev1 % mod + prev2 % mod) % mod;
prev1 = prev2;
prev2 = curr;
}
return curr;
}
let n = 2;
console.log(count_special(n));
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Last Updated :
24 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...