Lexicographically smallest string which is not a subsequence of given string
Last Updated :
23 Jul, 2025
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 <bits/stdc++.h>
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 the 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 the 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 the 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
C#
// C# program equivalent to the above Java program
using System;
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[i] == 'a')
freq++;
// Initialize string consisting of freq number of
// 'a'
string ans = "";
for (int i = 0; i < freq; i++) {
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 the answer string
return ans;
}
// Driver Code
static void Main(string[] args)
{
string S = "abcdefghijklmnopqrstuvwxyz";
int N = S.Length;
// Function call
Console.WriteLine(SmallestNonSubsequence(S, N));
}
}
// This code is contributed by phasing17
JavaScript
<script>
// JavaScript program for the above approach
// Function to find lexicographically smallest string that
// is not a subsequence of S
function smallestNonSubsequence(S,N)
{
// variable to store frequency of 'a'
let freq = 0;
// calculate frequency of 'a'
for (let i = 0; i < N; i++)
if (S[i] == 'a')
freq++;
// Initialize string consisting of freq number of 'a'
let ans = new Array(freq).fill('a').join('');
// change the last digit to 'b'
if (freq == N)
ans[freq - 1] = 'b';
// add another 'a' to the string
else
ans += 'a';
// return the answer string
return ans;
}
// Driver code
// Input
let S = "abcdefghijklmnopqrstuvwxyz";
let N = S.length;
// Function call
document.write(smallestNonSubsequence(S, N),"</br>");
// This code is contributed by shinjanpatra
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem