Related Articles

# Lexicographically smallest string which is not a subsequence of given string

• Last Updated : 05 Jul, 2021

Given a string S, the task is to find the string which is lexicographically smallest and not a subsequence of the given string S.

Examples:

Input: S = “abcdefghijklmnopqrstuvwxyz”
Output:
aa
Explanation:
String “aa” is the lexicographically smallest string which is not present in the given string as a subsequence.

Input: S = “aaaa”
Output:
aaab
Explanation:
String “aaa” is the lexicographically smallest string which is not present in the given string as a subsequence.

Approach: The idea is to solve the given problem is to find the frequency of the character ‘a’ in the given string (say f). If the value of f is less than the length of the string, then the answer would be the string formed by concatenating ‘a’ f+1 number of times as aaa….(f+1 times) will be the required lexicographically smallest string which is not a subsequence. Otherwise, the answer would be the string formed by concatenating ‘a’ (f-1 times) and ‘b’ at the end of the string.

## C++

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

## Java

 `// Java program for the above approach``import` `java.util.*;``public` `class` `GFG``{` `// Function to find lexicographically smallest string that``// is not a subsequence of S``static` `String smallestNonSubsequence(String S, ``int` `N)``{``  ` `    ``// variable to store frequency of 'a'``    ``int` `freq = ``0``;``  ` `    ``// calculate frequency of 'a'``    ``for` `(``int` `i = ``0``; i < N; i++)``        ``if` `(S.charAt(i) == ``'a'``)``            ``freq++;``  ` `    ``// Initialize string consisting of freq number of 'a'`` ``String ans=``""``;``    ``for``(``int` `i = ``0``; i < f req; i++){``        ``ans += ``'a'``;``    ``}``  ` `    ``// change the last digit to 'b'``    ``if` `(freq == N)``       ``ans = ans.replace(ans.charAt(freq - ``1``), ``'b'``);``  ` `    ``// add another 'a' to the string``    ``else``        ``ans += ``'a'``;``  ` `    ``// return tha answer string``    ``return` `ans;``}` `// Driver Code``public` `static` `void` `main(String args[])``{``    ``String S = ``"abcdefghijklmnopqrstuvwxyz"``;``    ``int` `N = S.length();` `    ``// Function call``    ``System.out.println(smallestNonSubsequence(S, N));``    ``}``}` `// This code is contributed by SoumikMondal`

## Python3

 `# Python3 program for the above approach` `# Function to find lexicographically smallest``# string that is not a subsequence of S``def` `smallestNonSubsequence(S, N):``    ` `    ``# Variable to store frequency of 'a'``    ``freq ``=` `0``    ` `    ``# Calculate frequency of 'a'``    ``for` `i ``in` `range``(N):``        ``if` `(S[i] ``=``=` `'a'``):``            ``freq ``+``=` `1``            ` `    ``# Initialize string consisting``    ``# of freq number of 'a'``    ``ans ``=` `""``    ``for` `i ``in` `range``(freq):``        ``ans ``+``=` `'a'` `    ``# Change the last digit to 'b'``    ``if` `(freq ``=``=` `N):``        ``ans ``=` `ans.replace(ans[freq ``-` `1``], ``'b'``)``        ` `    ``# Add another 'a' to the string``    ``else``:``        ``ans ``+``=` `'a'``        ` `    ``# Return tha answer string``    ``return` `ans` `# Driver code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``# Input``    ``S ``=` `"abcdefghijklmnopqrstuvwxyz"``    ``N ``=` `len``(S)` `    ``# Function call``    ``print``(smallestNonSubsequence(S, N))``    ` `# This code is contributed by SURENDRA_GANGWAR`
Output
`aa`

Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up