Count of distinct possible strings after performing given operations | C ++ implementation of the above approach ; Function that prints the number of different strings that can be formed ; Computing the length of the given string ; Base case ; Traverse the given string ; If two consecutive 1 ' s ▁ ▁ or ▁ 2' s are present ; Otherwise take the previous value ; Driver Code
#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 ] << " STRNEWLINE " ; } int main ( ) { string S = "0111022110" ; differentStrings ( S ) ; return 0 ; }