Lexicographically smallest subsequence possible by removing a character from given string
Given a string S of length N, the task is to find the lexicographically smallest subsequence of length (N – 1), i.e. by removing a single character from the given string.
Examples:
Input: S = “geeksforgeeks”
Output: “eeksforgeeks”
Explanation: Lexicographically smallest subsequence possible is “eeksforgeeks”.
Input: S = “zxvsjas”
Output: “xvsjas”
Explanation: Lexicographically smallest subsequence possible is “xvsjas”.
Naive Approach: The simplest approach is to generate all possible subsequences of length (N – 1) from the given string and store all subsequences in an array. Now, sort the array and print the string at 0th position for the smallest lexicographically subsequence.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void firstSubsequence(string s)
{
vector<string> allsubseq;
string k;
for ( int i = 0; i < s.length(); i++) {
k = s;
k.erase(i, 1);
allsubseq.push_back(k);
}
sort(allsubseq.begin(),
allsubseq.end());
cout << allsubseq[0];
}
int main()
{
string S = "geeksforgeeks" ;
firstSubsequence(S);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void firstSubsequence(String s)
{
Vector<String> allsubseq = new Vector<>();
for ( int i = 0 ; i < s.length(); i++)
{
String k = "" ;
for ( int j = 0 ; j < s.length(); j++)
{
if (i != j)
{
k += s.charAt(j);
}
}
allsubseq.add(k);
}
Collections.sort(allsubseq);
System.out.print(allsubseq.get( 0 ));
}
public static void main(String[] args)
{
String S = "geeksforgeeks" ;
firstSubsequence(S);
}
}
|
Python3
def firstSubsequence(s):
allsubseq = []
k = []
for i in range ( len (s)):
k = [i for i in s]
del k[i]
allsubseq.append("".join(k))
allsubseq = sorted (allsubseq)
print (allsubseq[ 0 ])
if __name__ = = '__main__' :
S = "geeksforgeeks"
firstSubsequence(S)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void firstSubsequence( string s)
{
List< string > allsubseq = new List< string >();
for ( int i = 0; i < s.Length; i++)
{
string k = "" ;
for ( int j = 0; j < s.Length; j++)
{
if (i != j)
{
k += s[j];
}
}
allsubseq.Add(k);
}
allsubseq.Sort();
Console.WriteLine(allsubseq[0]);
}
public static void Main()
{
string S = "geeksforgeeks" ;
firstSubsequence(S);
}
}
|
Javascript
<script>
function firstSubsequence(s)
{
let allsubseq = [];
for (let i = 0; i < s.length; i++)
{
let k = "" ;
for (let j = 0; j < s.length; j++)
{
if (i != j)
{
k += s[j];
}
}
allsubseq.push(k);
}
(allsubseq).sort();
document.write(allsubseq[0]);
}
let S = "geeksforgeeks" ;
firstSubsequence(S);
</script>
|
Output:
eeksforgeeks
Time Complexity: O(N *N)
Auxiliary Space: O(N)
Efficient Approach: To optimize the above approach, the idea is to iterate over the string and check if the ith character is greater that (i + 1)th character, then simply remove the ith character and print the remaining string. Otherwise, remove the last element and print the desired subsequence.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void firstSubsequence(string s)
{
int isMax = -1;
for ( int i = 0;
i < s.length() - 1; i++) {
if (s[i] > s[i + 1]) {
isMax = i;
break ;
}
}
if (isMax >= 0) {
s.erase(isMax, 1);
}
else {
s.erase(s.length() - 1, 1);
}
cout << s;
}
int main()
{
string S = "geeksforgeeks" ;
firstSubsequence(S);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void firstSubsequence(String s)
{
int isMax = - 1 ;
for ( int i = 0 ; i < s.length() - 1 ; i++)
{
if (s.charAt(i) > s.charAt(i + 1 ))
{
isMax = i;
break ;
}
}
if (isMax >= 0 )
{
s = s.substring( 0 , isMax) +
s.substring(isMax + 1 );
}
else
{
s = s.substring( 0 , s.length() - 1 );
}
System.out.print(s);
}
public static void main(String[] args)
{
String S = "geeksforgeeks" ;
firstSubsequence(S);
}
}
|
Python3
def firstSubsequence(s):
isMax = - 1
for i in range ( len (s)):
if (s[i] > s[i + 1 ]):
isMax = i
break
if (isMax > = 0 ):
s = s[ 0 : isMax] + s[isMax + 1 : len (s)]
else :
s = s[ 0 : s.length() - 1 ]
print (s)
if __name__ = = '__main__' :
S = "geeksforgeeks"
firstSubsequence(S)
|
C#
using System;
class GFG{
static void firstSubsequence(String s)
{
int isMax = -1;
for ( int i = 0; i < s.Length - 1; i++)
{
if (s[i] > s[i + 1])
{
isMax = i;
break ;
}
}
if (isMax >= 0)
{
s = s.Substring(0, isMax) +
s.Substring(isMax + 1);
}
else
{
s = s.Substring(0, s.Length - 1);
}
Console.Write(s);
}
public static void Main(String[] args)
{
String S = "geeksforgeeks" ;
firstSubsequence(S);
}
}
|
Javascript
<script>
function firstSubsequence(s)
{
let isMax = -1;
for (let i = 0; i < s.length - 1; i++)
{
if (s[i] > s[i + 1])
{
isMax = i;
break ;
}
}
if (isMax >= 0)
{
s = s.substring(0, isMax) +
s.substring(isMax + 1);
}
else
{
s = s.substring(0, s.length - 1);
}
document.write(s);
}
let S = "geeksforgeeks" ;
firstSubsequence(S);
</script>
|
Output:
eeksforgeeks
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
19 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...