Count of distinct possible strings after performing given operations | 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
def differentStrings ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE dp = [ 0 ] * ( n + 1 ) NEW_LINE dp [ 0 ] = dp [ 1 ] = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( s [ i ] == s [ i - 1 ] and ( s [ i ] == '1' or s [ i ] == '2' ) ) : NEW_LINE INDENT dp [ i + 1 ] = dp [ i ] + dp [ i - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i + 1 ] = dp [ i ] NEW_LINE DEDENT DEDENT print ( dp [ n ] ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = "0111022110" NEW_LINE differentStrings ( S ) NEW_LINE DEDENT