Open In App

Lexicographically largest subsequence such that every character occurs at least k times

Improve
Improve
Like Article
Like
Save
Share
Report

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:

Check if a line touches or intersects a circle

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++





Java




import java.util.Arrays;
 
 
// Java program to find lexicographically largest
// subsequence where every character appears at
// least k times. 
class GFG {
 
// Find lexicographically largest subsequence of
// s[0..n-1] such that every character appears
// at least k times. The result is filled in t[]
static void subsequence(char s[], char t[], int n, int k)
{
    int last = 0, cnt = 0, new_last = 0, size = 0;
  
    // Starting from largest character 'z' to 'a'
    for (char ch = 'z'; ch >= 'a'; ch--) {
        cnt = 0;
  
        // Counting the frequency of the character
        for (int i = last; i < n; i++) {
            if (s[i] == ch)
                cnt++;
        }
  
        // If frequency is greater than k
        if (cnt >= k) {
  
            // From the last point we leave
            for (int i = last; i < n; i++) {
  
                // check if string contain ch
                if (s[i] == ch) {
  
                    // If yes, append to output string
                    t[size++] = ch;
                    new_last = i;
                }
            }
  
            // Update the last point.
            last = new_last;
        }
    }
    t[size] = '\0';
}
  
// Driver code
    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]);
     
    }
}
 
// This code is contributed by Jajput-Ji


Python3





C#





Javascript





Output

nn

Time Complexity: O(n)
Auxiliary Space: O(n)

 



Last Updated : 20 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads