Lexicographically smallest string which is not a subsequence of given string | 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
def smallestNonSubsequence ( S , N ) : NEW_LINE INDENT freq = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( S [ i ] == ' a ' ) : NEW_LINE INDENT freq += 1 NEW_LINE DEDENT DEDENT ans = " " NEW_LINE for i in range ( freq ) : NEW_LINE INDENT ans += ' a ' NEW_LINE DEDENT if ( freq == N ) : NEW_LINE INDENT ans = ans . replace ( ans [ freq - 1 ] , ' b ' ) NEW_LINE DEDENT else : NEW_LINE INDENT ans += ' a ' NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " abcdefghijklmnopqrstuvwxyz " NEW_LINE N = len ( S ) NEW_LINE print ( smallestNonSubsequence ( S , N ) ) NEW_LINE DEDENT