Minimum number whose binary form is not a subsequence of given binary string | Function to check if string str1 is a subsequence of string str2 ; Store index of str1 ; Traverse str2 and str1 , and compare current character of str2 with first unmatched char of str1 ; If matched , move ahead in str1 ; If all characters of str1 were found in str2 ; Function to find the minimum number which is not a subsequence of the given binary string in its binary form ; Store the decimal number of string , S ; Initialize the required result ; Iterate in the range [ 0 , R ] ; Convert integer i to binary string ; Check if the string is not a subsequence ; Update ans and break ; Print the required result ; Driver Code ; Function Call ; Function Call
def isSubsequence ( str1 , str2 , m , n ) : NEW_LINE INDENT j = 0 NEW_LINE i = 0 NEW_LINE while ( i < n and j < m ) : NEW_LINE INDENT if ( str1 [ j ] == str2 [ i ] ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return ( j == m ) NEW_LINE DEDENT def findMinimumNumber ( s ) : NEW_LINE INDENT r = int ( s , 2 ) NEW_LINE ans = r + 1 NEW_LINE for i in range ( r + 1 ) : NEW_LINE INDENT p = " " NEW_LINE j = i NEW_LINE while ( j > 0 ) : NEW_LINE INDENT p += str ( j % 2 ) NEW_LINE j = j // 2 NEW_LINE DEDENT m = len ( p ) NEW_LINE n = len ( s ) NEW_LINE p = p [ : : - 1 ] NEW_LINE if ( isSubsequence ( p , s , m , n ) == False ) : NEW_LINE INDENT ans = i NEW_LINE break NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "10101" NEW_LINE findMinimumNumber ( s ) NEW_LINE DEDENT