Smallest string which not a subsequence of the given string
Last Updated :
09 Jun, 2021
Given a string str, consisting of lowercase alphabets, the task is to find the shortest string which is not a subsequence of the given string. If multiple strings exist, then print any one of them.
Examples:
Input: str = “abaabcc”
Output: d
Explanation:
One of the possible shortest string which is not a subsequence of the given string is “d”. Therefore, the required output is “d”.
Input: str = “abcdefghijklmnopqrstuvwxyzaabbccdd”
Output: ze
Approach: The problem can be solved using Greedy technique. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string ShortestSubsequenceNotPresent(string str)
{
string shortestString;
int N = str.length();
unordered_set< char > subsegments;
for ( int i = 0; i < N; i++) {
subsegments.insert(str[i]);
if (subsegments.size() == 26) {
shortestString.push_back(str[i]);
subsegments.clear();
}
}
for ( char ch = 'a' ; ch <= 'z' ; ch++) {
if (subsegments.count(ch) == 0) {
shortestString.push_back(ch);
return shortestString;
}
}
return shortestString;
}
int main()
{
string str
= "abcdefghijklmnopqrstuvwxyzaabbccdd" ;
cout << ShortestSubsequenceNotPresent(str);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static String
ShortestSubsequenceNotPresent(String str)
{
String shortestString = "" ;
int N = str.length();
HashSet<Character> subsegments = new HashSet<>();
for ( int i = 0 ; i < N; i++) {
subsegments.add(str.charAt(i));
if (subsegments.size() == 26 ) {
shortestString
= shortestString + str.charAt(i);
subsegments.clear();
}
}
for ( char ch = 'a' ; ch <= 'z' ; ch++) {
if (!subsegments.contains(ch)) {
shortestString = shortestString + ch;
return shortestString;
}
}
return shortestString;
}
public static void main(String[] args)
{
String str = "abcdefghijklmnopqrstuvwxyzaabbccdd" ;
System.out.print(
ShortestSubsequenceNotPresent(str));
}
}
|
Python3
def ShortestSubsequenceNotPresent( Str ):
shortestString = ""
N = len ( Str )
subsegments = set ()
for i in range (N):
subsegments.add( Str [i])
if ( len (subsegments) = = 26 ) :
shortestString + = Str [i]
subsegments.clear()
for ch in range ( int ( 26 )):
if ( chr (ch + 97 ) not in subsegments) :
shortestString + = chr (ch + 97 )
return shortestString
return shortestString
Str = "abcdefghijklmnopqrstuvwxyzaabbccdd"
print (ShortestSubsequenceNotPresent( Str ))
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static String ShortestSubsequenceNotPresent(
String str)
{
String shortestString = "" ;
int N = str.Length;
HashSet< char > subsegments = new HashSet< char >();
for ( int i = 0; i < N; i++)
{
subsegments.Add(str[i]);
if (subsegments.Count == 26)
{
shortestString = shortestString + str[i];
subsegments.Clear();
}
}
for ( char ch = 'a' ; ch <= 'z' ; ch++)
{
if (!subsegments.Contains(ch))
{
shortestString = shortestString + ch;
return shortestString;
}
}
return shortestString;
}
public static void Main(String[] args)
{
String str = "abcdefghijklmnopqrstuvwxyzaabbccdd" ;
Console.Write(
ShortestSubsequenceNotPresent(str));
}
}
|
Javascript
<script>
function ShortestSubsequenceNotPresent(str)
{
var shortestString = [];
var N = str.length;
var subsegments = new Set();
for ( var i = 0; i < N; i++) {
subsegments.add(str[i].charCodeAt(0));
if (subsegments.size == 26) {
shortestString.push(str[i]);
subsegments = new Set();
}
}
for ( var ch = 'a' .charCodeAt(0);
ch <= 'z' .charCodeAt(0); ch++) {
if (!subsegments.has(ch)) {
shortestString.push(String.fromCharCode(ch));
return shortestString.join( "" );
}
}
return shortestString.join( "" );
}
var str = "abcdefghijklmnopqrstuvwxyzaabbccdd" ;
document.write( ShortestSubsequenceNotPresent(str));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...