Count of non | Function to return the count of required non - overlapping sub - strings ; To store the required count ; If "010" matches the sub - string starting at current index i ; If "101" matches the sub - string starting at current index i ; Driver code
def countSubStr ( s , n ) : NEW_LINE INDENT count = 0 ; NEW_LINE i = 0 NEW_LINE while i < ( n - 2 ) : NEW_LINE INDENT if ( s [ i ] == '0' and s [ i + 1 ] == '1' and s [ i + 2 ] == '0' ) : NEW_LINE INDENT count += 1 ; NEW_LINE i += 3 ; NEW_LINE DEDENT elif ( s [ i ] == '1' and s [ i + 1 ] == '0' and s [ i + 2 ] == '1' ) : NEW_LINE INDENT count += 1 ; NEW_LINE i += 3 ; NEW_LINE DEDENT else : NEW_LINE INDENT i += 1 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "10101010101" ; NEW_LINE n = len ( s ) ; NEW_LINE print ( countSubStr ( s , n ) ) ; NEW_LINE DEDENT