Minimum number whose binary form is not a subsequence of given binary string | C ++ program for the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isSubsequence ( string str1 , string str2 , int m , int n ) { int j = 0 ; for ( int i = 0 ; i < n && j < m ; i ++ ) if ( str1 [ j ] == str2 [ i ] ) j ++ ; return ( j == m ) ; } void findMinimumNumber ( string s ) { int r = stoi ( s , 0 , 2 ) ; int ans = r + 1 ; for ( int i = 0 ; i <= r ; i ++ ) { string p = " " ; int j = i ; while ( j > 0 ) { p += to_string ( j % 2 ) ; j = j / 2 ; } int m = p . length ( ) ; int n = s . length ( ) ; reverse ( p . begin ( ) , p . end ( ) ) ; if ( ! isSubsequence ( p , s , m , n ) ) { ans = i ; break ; } } cout << ans ; } int main ( ) { string s = "10101" ; findMinimumNumber ( s ) ; return 0 ; }