Lexicographically largest possible String after removal of K characters
Last Updated :
27 May, 2021
Given a string S consisting of only lowercase letters, the task is to find the lexicographically largest string that can be obtained by removing K characters from the given string.
Examples:
Input: s = “zyxedcba”, K=1
Output: zyxedcb
Explanation: The character with the smallest ASCII value from the given string is ‘a’.
Removal of ‘a’ generates the lexicographically largest possible string.
Input: s = “abcde”, K=2
Output: cde
Approach:
The idea is to use Stack Data Structure it to solve the problem. Follow the steps below to solve the problem:
- Traverse the string.
- For every character, check if it is greater than the character at the top of the stack. If found to be true, pop the top element of the stack if K > 0.
- Insert the character into the stack.
- After completing the traversal of the string, if K > 0, then remove the top K elements of the stack.
- Finally, store the characters in the stack as the answer. Print the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string largestString(string num, int k)
{
string ans = "" ;
for ( auto i : num) {
while (ans.length() && ans.back() < i
&& k > 0) {
ans.pop_back();
k--;
}
ans.push_back(i);
}
while (ans.length() and k--) {
ans.pop_back();
}
return ans;
}
int main()
{
string str = "zyxedcba" ;
int k = 1;
cout << largestString(str, k) << endl;
}
|
Java
class GFG{
static String largestString(String num, int k)
{
String ans = "" ;
for ( char i : num.toCharArray())
{
while (ans.length() > 0 &&
ans.charAt(ans.length() - 1 ) < i &&
k > 0 )
{
ans = ans.substring( 0 , ans.length() - 1 );
k--;
}
ans += i;
}
while (ans.length() > 0 && k-- > 0 )
{
ans = ans.substring( 0 , ans.length() - 1 );
}
return ans;
}
public static void main(String[] args)
{
String str = "zyxedcba" ;
int k = 1 ;
System.out.print(largestString(str, k) + "\n" );
}
}
|
Python3
def largestString(num, k):
ans = []
for i in range ( len (num)):
while ( len (ans) and ans[ - 1 ] < num[i] and
k > 0 ):
ans.pop()
k - = 1
ans.append(num[i])
while ( len (ans) and k):
k - = 1
ans.pop()
return ans
str = "zyxedcba"
k = 1
print ( * largestString( str , k), sep = "")
|
C#
using System;
class GFG{
static String largestString(String num, int k)
{
String ans = "" ;
foreach ( char i in num.ToCharArray())
{
while (ans.Length > 0 &&
ans[ans.Length - 1] < i && k > 0)
{
ans = ans.Substring(0, ans.Length - 1);
k--;
}
ans += i;
}
while (ans.Length > 0 && k-- > 0)
{
ans = ans.Substring(0, ans.Length - 1);
}
return ans;
}
public static void Main(String[] args)
{
String str = "zyxedcba" ;
int k = 1;
Console.Write(largestString(str, k) + "\n" );
}
}
|
Javascript
<script>
function largestString(num, k) {
var ans = "" ;
var str = num.split( "" );
for (const i of str) {
while (ans.length > 0 && ans[ans.length - 1] < i && k > 0) {
ans = ans.substring(0, ans.length - 1);
k--;
}
ans += i;
}
while (ans.length > 0 && k-- > 0) {
ans = ans.substring(0, ans.length - 1);
}
return ans;
}
var str = "zyxedcba" ;
var k = 1;
document.write(largestString(str, k) + "<br>" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...