Lexicographically smallest string which is not a subsequence of given string | C ++ program for the above approach ; Function to find lexicographically smallest string that is not a subsequence of S ; variable to store frequency of ' a ' ; calculate frequency of ' a ' ; Initialize string consisting of freq number of ' a ' ; change the last digit to ' b ' ; add another ' a ' to the string ; return tha answer string ; Driver code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string smallestNonSubsequence ( string S , int N ) { int freq = 0 ; for ( int i = 0 ; i < N ; i ++ ) if ( S [ i ] == ' a ' ) freq ++ ; string ans ( freq , ' a ' ) ; if ( freq == N ) ans [ freq - 1 ] = ' b ' ; else ans += ' a ' ; return ans ; } int main ( ) { string S = " abcdefghijklmnopqrstuvwxyz " ; int N = S . length ( ) ; cout << smallestNonSubsequence ( S , N ) << endl ; return 0 ; }