Lexicographically smallest string which is not a subsequence of given string | Java 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
import java . util . * ; public class GFG { static String smallestNonSubsequence ( String S , int N ) { int freq = 0 ; for ( int i = 0 ; i < N ; i ++ ) if ( S . charAt ( i ) == ' a ' ) freq ++ ; String ans = " " ; for ( int i = 0 ; i < f req ; i ++ ) { ans += ' a ' ; } if ( freq == N ) ans = ans . replace ( ans . charAt ( freq - 1 ) , ' b ' ) ; else ans += ' a ' ; return ans ; } public static void main ( String args [ ] ) { String S = " abcdefghijklmnopqrstuvwxyz " ; int N = S . length ( ) ; System . out . println ( smallestNonSubsequence ( S , N ) ) ; } }