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 f_{z} times in S. If f_{z} >= 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 f_{z} = 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 became “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++

`// C++ program to find lexicographically largest ` `// subsequence where every character appears at ` `// least k times. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Find lexicographically largest subsequence of ` `// s[0..n-1] such that every character appears ` `// at least k times. The result is filled in t[] ` `void` `subsequence(` `char` `s[], ` `char` `t[], ` `int` `n, ` `int` `k) ` `{ ` ` ` `int` `last = 0, cnt = 0, new_last = 0, size = 0; ` ` ` ` ` `// Starting from largest charter '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 ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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 charter '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 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find lexicographically largest ` `# subsequence where every character appears at ` `# least k times. ` ` ` `# Find lexicographically largest subsequence of ` `# s[0..n-1] such that every character appears ` `# at least k times. The result is filled in t[] ` `def` `subsequence(s, t, n, k): ` ` ` `last ` `=` `0` ` ` `cnt ` `=` `0` ` ` `new_last ` `=` `0` ` ` `size ` `=` `0` ` ` ` ` `string ` `=` `'zyxwvutsrqponmlkjihgfedcba'` ` ` ` ` `# Starting from largest charter 'z' to 'a' ` ` ` `for` `ch ` `in` `string: ` ` ` `cnt ` `=` `0` ` ` `for` `i ` `in` `range` `(last, n): ` ` ` `if` `s[i] ` `=` `=` `ch: ` ` ` `cnt ` `+` `=` `1` ` ` ` ` `# If frequency is greater than k ` ` ` `if` `cnt >` `=` `k: ` ` ` ` ` `# From the last point we leave ` ` ` `for` `i ` `in` `range` `(last, n): ` ` ` ` ` `# check if string contain ch ` ` ` `if` `s[i] ` `=` `=` `ch: ` ` ` ` ` `# If yes, append to output string ` ` ` `t[size] ` `=` `ch ` ` ` `new_last ` `=` `i ` ` ` `size ` `+` `=` `1` ` ` ` ` `# Update the last point. ` ` ` `last ` `=` `new_last ` ` ` `# Driver Code ` `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) ` ` ` `# This code is contributed by ` `# sanjeev2552 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find lexicographically ` `// largest subsequence where every ` `// character appears at least k times. ` `using` `System; ` ` ` `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() ` `{ ` ` ` `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]); ` `} ` `} ` ` ` `// This code contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

**Output:**

nn

This article is contributed by **Anuj Chauhan (anuj0503)**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Longest subsequence where each character occurs at least k times
- Queries to print the character that occurs the maximum number of times in a given range
- Find the lexicographically largest palindromic Subsequence of a String
- Find number of times a string occurs as a subsequence in given string
- Longest subsequence where every character appears at-least k times
- Find a string such that every character is lexicographically greater than its immediate next character
- Number of times the given string occurs in the array in the range [l, r]
- Maximum length prefix of one string that occurs as subsequence in another
- Replace every character of string by character whose ASCII value is K times more than it
- Number of permutations of a string in which all the occurrences of a given character occurs together
- Lexicographically next greater string using same character set
- Lexicographically smallest string formed by removing at most one character
- Lexicographically smaller string by swapping at most one character pair
- Lexicographically largest string possible in one swap
- Lexicographically largest sub-sequence of the given string
- Make the string lexicographically smallest and non palindromic by replacing exactly one character
- Lexicographically smallest and largest substring of size k
- Lexicographically largest possible String after removal of K characters
- Lexicographically largest string formed from the characters in range L and R
- Lexicographically largest prime path from top-left to bottom-right in a matrix