Count the number of Special Strings of a given length N | Java program to count the number of special strings of a given length N ; Function to return count of special Strings ; Stores the answer for a particular value of n ; For n = 0 we have empty String ; For n = 1 we have 2 special Strings ; Calculate count of special String of length i ; fib [ n ] stores the count of special Strings of length n ; Driver code ; Initialise n
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 ) + "NEW_LINE"); } }