Given a string S and an integer K. The task is to find lexicographically largest subsequence of S, say T, such that every character in T must occur at least K times.
Examples:
Input : S = "banana", K = 2.
Output : nn
Possible subsequence where each character exists at least 2 times are:

From the above subsequences, "nn" is the lexicographically largest.
The idea is to solve greedily the above problem. If we want to make the subsequence lexicographically largest, we must give priority to lexicographically larger characters. ‘z’ is the largest character, let suppose z occurs fz times in S. If fz >= K, append ‘z’z k times in the string T and keep removing characters from the left of S until all the z’s are removed. Apply the strategy with ‘y’, ‘w’, ….., ‘a’. In the end, you will find the answer.
Let see an example. Suppose S = “zzwzawa” and K = 2. Start with the largest character ‘z’. Here fz = 3 >= K. So T will become “zzz” and we will remove letters from the left of S until all the z’s are removed. So now S will become “awa”. Next largest is ‘y’ but that occurs 0 times in k so we will skip it. We will skip ‘w’, ‘v’ etc also until we go to ‘a’ which occurs 2 times. Now T will become “zzzaa” and S will become a empty string. Our answer is “zzzaa”.
Below is implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
void subsequence( char s[], char t[], int n, int k)
{
int last = 0, cnt = 0, new_last = 0, size = 0;
for ( char ch = 'z' ; ch >= 'a' ; ch--) {
cnt = 0;
for ( int i = last; i < n; i++) {
if (s[i] == ch)
cnt++;
}
if (cnt >= k) {
for ( int i = last; i < n; i++) {
if (s[i] == ch) {
t[size++] = ch;
new_last = i;
}
}
last = new_last;
}
}
t[size] = '\0' ;
}
int main()
{
char s[] = "banana" ;
int n = sizeof (s);
int k = 2;
char t[n];
subsequence(s, t, n - 1, k);
cout << t << endl;
return 0;
}
|
Java
import java.util.Arrays;
class GFG {
static void subsequence( char s[], char t[], int n, int k)
{
int last = 0 , cnt = 0 , new_last = 0 , size = 0 ;
for ( char ch = 'z' ; ch >= 'a' ; ch--) {
cnt = 0 ;
for ( int i = last; i < n; i++) {
if (s[i] == ch)
cnt++;
}
if (cnt >= k) {
for ( int i = last; i < n; i++) {
if (s[i] == ch) {
t[size++] = ch;
new_last = i;
}
}
last = new_last;
}
}
t[size] = '\0' ;
}
public static void main(String[] args) {
char s[] = { 'b' , 'a' , 'n' , 'a' , 'n' , 'a' };
int n = s.length;
int k = 2 ;
char t[] = new char [n];
subsequence(s, t, n - 1 , k);
for ( int i = 0 ;i<t.length;i++)
if (t[i]!= 0 )
System.out.print(t[i]);
}
}
|
Python3
def subsequence(s, t, n, k):
last = 0
cnt = 0
new_last = 0
size = 0
string = 'zyxwvutsrqponmlkjihgfedcba'
for ch in string:
cnt = 0
for i in range (last, n):
if s[i] = = ch:
cnt + = 1
if cnt > = k:
for i in range (last, n):
if s[i] = = ch:
t[size] = ch
new_last = i
size + = 1
last = new_last
if __name__ = = "__main__" :
s = [ 'b' , 'a' , 'n' , 'a' , 'n' , 'a' ]
n = len (s)
k = 2
t = [''] * n
subsequence(s, t, n - 1 , k)
t = ''.join(t)
print (t)
|
C#
using System;
class GFG
{
static void subsequence( char []s, char []t,
int n, int k)
{
int last = 0, cnt = 0,
new_last = 0, size = 0;
for ( char ch = 'z' ; ch >= 'a' ; ch--)
{
cnt = 0;
for ( int i = last; i < n; i++)
{
if (s[i] == ch)
cnt++;
}
if (cnt >= k)
{
for ( int i = last; i < n; i++)
{
if (s[i] == ch)
{
t[size++] = ch;
new_last = i;
}
}
last = new_last;
}
}
t[size] = '\0' ;
}
public static void Main()
{
char []s = { 'b' , 'a' , 'n' , 'a' , 'n' , 'a' };
int n = s.Length;
int k = 2;
char []t = new char [n];
subsequence(s, t, n - 1, k);
for ( int i = 0; i < t.Length; i++)
if (t[i] != 0)
Console.Write(t[i]);
}
}
|
Javascript
<script>
function subsequence(s, t, n, k)
{
var last = 0, cnt = 0, new_last = 0, size = 0;
for ( var ch = 'z' .charCodeAt(0);
ch >= 'a' .charCodeAt(0); ch--)
{
cnt = 0;
for ( var i = last; i < n; i++) {
if (s[i].charCodeAt(0) == ch)
cnt++;
}
if (cnt >= k) {
for ( var i = last; i < n; i++) {
if (s[i].charCodeAt(0) == ch) {
t[size++] = String.fromCharCode(ch);
new_last = i;
}
}
last = new_last;
}
}
}
var s = "banana" ;
var n = s.length;
var k = 2;
var t = Array(n);
subsequence(s, t, n - 1, k);
document.write( t.join( '' ) );
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
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 :
20 Jul, 2022
Like Article
Save Article