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++
#include <bits/stdc++.h>
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;
}
|
Java
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));
}
}
|
Python3
def smallestNonSubsequence(S, N):
freq = 0
for i in range (N):
if (S[i] = = 'a' ):
freq + = 1
ans = ""
for i in range (freq):
ans + = 'a'
if (freq = = N):
ans = ans.replace(ans[freq - 1 ], 'b' )
else :
ans + = 'a'
return ans
if __name__ = = '__main__' :
S = "abcdefghijklmnopqrstuvwxyz"
N = len (S)
print (smallestNonSubsequence(S, N))
|
C#
using System;
class GFG
{
static string SmallestNonSubsequence( string S, int N)
{
int freq = 0;
for ( int i = 0; i < N; i++)
if (S[i] == 'a' )
freq++;
string ans = "" ;
for ( int i = 0; i < freq; i++) {
ans += 'a' ;
}
if (freq == N)
ans = ans.Replace(ans[freq - 1], 'b' );
else
ans += 'a' ;
return ans;
}
static void Main( string [] args)
{
string S = "abcdefghijklmnopqrstuvwxyz" ;
int N = S.Length;
Console.WriteLine(SmallestNonSubsequence(S, N));
}
}
|
Javascript
<script>
function smallestNonSubsequence(S,N)
{
let freq = 0;
for (let i = 0; i < N; i++)
if (S[i] == 'a' )
freq++;
let ans = new Array(freq).fill( 'a' ).join( '' );
if (freq == N)
ans[freq - 1] = 'b' ;
else
ans += 'a' ;
return ans;
}
let S = "abcdefghijklmnopqrstuvwxyz" ;
let N = S.length;
document.write(smallestNonSubsequence(S, N), "</br>" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)