Given string str of length N and an integer K, the task is to return the largest string in Dictionary Order by erasing K characters from that string.
A largest string Dictionary order is the last string when strings are arranged in alphabetical order.
Examples:
Input: str = “ritz” K = 2
Output: tz
Explanation:
There are 6 possible ways of deleting two characters from s: “ri”, “rt”, “rz”, “it”, “iz”, “tz”.
Among these strings “tz” is the largest in dictionary order.
Thus “tz” is the desired output.
Input: str = “jackie” K = 2
Output: jkie
Explanation:
The characters “a” and “c” are deleted to get the largest possible string.
Naive Approach: The idea is to find all the subsequence of the given string length N – K. Store those subsequences in a list. There will be nCm Such sequences. After the above steps print the largest string in alphabetical order stored in the list.
Time Complexity: O(2N-K)
Efficient Approach: The idea is to use a Deque. Below are the steps:
- Store all the characters of the string in the deque.
- Traverse the given string and for each character in the string keep popping the characters from deque if it is less than the last character stored in the deque. Perform this operation until K is non-zero.
- Now after the above operations, insert the current character in the deque.
- After the above operations, the string formed by the characters stored in the deque is the resultant string.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string largestString( int n, int k, string s)
{
deque< char > deq;
for ( int i = 0; i < n; ++i)
{
while (deq.size() > 0 &&
deq.back() < s[i] &&
k > 0)
{
deq.pop_front();
k--;
}
deq.push_back(s[i]);
}
string st = "" ;
for ( char c : deq)
st = st + c;
return st;
}
int main()
{
int n = 4;
int k = 2;
string sc = "ritz" ;
string result = largestString(n, k, sc);
cout << result << endl;
return 0;
}
|
Java
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;
import java.io.IOException;
public class GFG {
public static String
largestString( int n, int k, String sc)
{
char [] s = sc.toCharArray();
Deque<Character> deq
= new ArrayDeque<>();
for ( int i = 0 ; i < n; ++i) {
while (deq.size() > 0
&& deq.getLast() < s[i]
&& k > 0 ) {
deq.pollLast();
k--;
}
deq.add(s[i]);
}
String st = "" ;
for ( char c : deq)
st = st + Character.toString(c);
return st;
}
public static void main(String[] args)
throws IOException
{
int n = 4 ;
int k = 2 ;
String sc = "ritz" ;
String result = largestString(n, k, sc);
System.out.println(result);
}
}
|
Python3
from collections import deque
def largestString(n, k, sc):
s = [i for i in sc]
deq = deque()
for i in range (n):
while ( len (deq) > 0 and
deq[ - 1 ] < s[i] and
k > 0 ):
deq.popleft()
k - = 1
deq.append(s[i])
st = ""
for c in deq:
st = st + c
return st
if __name__ = = '__main__' :
n = 4
k = 2
sc = "ritz"
result = largestString(n, k, sc)
print (result)
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
class GFG
{
public static string LargestString( int n, int k, string sc)
{
char [] s = sc.ToCharArray();
List< char > deq = new List< char >();
for ( int i = 0; i < n; ++i)
{
while (deq.Count > 0
&& deq[deq.Count - 1] < s[i]
&& k > 0)
{
deq.RemoveAt(deq.Count - 1);
k--;
}
deq.Add(s[i]);
}
string st = "" ;
foreach ( char c in deq)
st = st + c.ToString();
return st;
}
public static void Main( string [] args)
{
int n = 4;
int k = 2;
string sc = "ritz" ;
string result = LargestString(n, k, sc);
Console.WriteLine(result);
}
}
|
Javascript
<script>
function largestString(n, k, s)
{
var deq = [];
for ( var i = 0; i < n; ++i)
{
while (deq.length > 0 &&
deq[deq.length - 1] < s[i] &&
k > 0)
{
deq.shift();
k--;
}
deq.push(s[i]);
}
var st = "" ;
deq.forEach(c => {
st = st + c;
});
return st;
}
var n = 4;
var k = 2;
var sc = "ritz" ;
var result = largestString(n, k, sc);
document.write(result);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
17 Feb, 2023
Like Article
Save Article