# Longest subsequence where each character occurs at least k times

Given a string ‘s’ and an integer k, find other string ‘t’ such that ‘t’ is the largest subsequence of given string ‘s’ and each character of ‘t’ must occur at least k times in string s.

Examples :

```Input : s = "geeksforgeeks"
k = 2
Output : geeksgeeks

Input : s = "baaabaacba"
k = 3
Output : baaabaaba
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

A simple solution is to generate all subsequences. For every subsequence, check if it has all characters at least k times. Find longest such subsequence. Time complexity of this approach is exponential.

Efficient Approach We can take another array to keep the record of count of each character from string s, if any character occurred more than or equal to k times, then we simply print it.

## CPP

 `// CPP Program to find the subsequence ` `// with each character occurring at least ` `// k times in string s ` `#include ` `using` `namespace` `std; ` ` `  `#define MAX_CHAR 26 ` ` `  `// Function to find the subsequence ` `void` `findSubsequence(string str, ``int` `k) ` `{ ` `    ``// Taking an extra array to keep ` `    ``// record for character count in s ` `    ``int` `a[MAX_CHAR] = { 0 }; ` ` `  `    ``// Counting occurrences of all  ` `    ``// characters in str[] ` `    ``for` `(``int` `i = 0; i < str.size(); i++)  ` `        ``a[str[i] - ``'a'``]++;     ` ` `  `    ``// Printing characters with count ` `    ``// >= k in same order as they appear ` `    ``// in str. ` `    ``for` `(``int` `i = 0; i < l; i++)  ` `        ``if` `(a[str[i] - ``'a'``] >= k) ` `            ``cout << str[i];     ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `k = 2; ` `    ``findSubsequence(``"geeksforgeeks"``, k); ` `    ``return` `0; ` `} `

## Java

 `// Java Program to find the subsequence ` `// with each character occurring at least ` `// k times in string s ` `class` `GFG { ` `     `  `    ``static` `final` `int` `MAX_CHAR = ``26``; ` `     `  `    ``// Function to find the subsequence ` `    ``static` `void` `findSubsequence(String str, ``int` `k)  ` `    ``{ ` `         `  `        ``// Taking an extra array to keep ` `        ``// record for character count in s ` `        ``int` `a[] = ``new` `int``[MAX_CHAR]; ` `     `  `        ``// Counting occurrences of all ` `        ``// characters in str[] ` `        ``for` `(``int` `i = ``0``; i < str.length(); i++) ` `            ``a[str.charAt(i) - ``'a'``]++; ` `     `  `        ``// Printing characters with count ` `        ``// >= k in same order as they appear ` `        ``// in str. ` `        ``for` `(``int` `i = ``0``; i < str.length(); i++) ` `            ``if` `(a[str.charAt(i) - ``'a'``] >= k) ` `                ``System.out.print(str.charAt(i)); ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) { ` `         `  `        ``int` `k = ``2``; ` `        ``findSubsequence(``"geeksforgeeks"``, k); ` `    ``} ` `} ` ` `  `// This code is contributed by Anant Agarwal. `

## Python3

 `# Python Program to find the subsequence ` `# with each character occurring at least ` `# k times in string s ` ` `  `MAX_CHAR ``=` `26` ` `  `# Function to find the subsequence ` `def` `findSubsequence(stri, k): ` `    ``# Taking an extra array to keep ` `    ``# record for character count in s ` `    ``a ``=` `[``0``] ``*` `MAX_CHAR; ` ` `  `    ``# Counting occurrences of all  ` `    ``# characters in str[] ` `    ``for` `i ``in` `range``(``len``(stri)):  ` `        ``a[``ord``(stri[i]) ``-` `ord``(``'a'``)] ``+``=` `1` ` `  `    ``# Printing characters with count ` `    ``# >= k in same order as they appear ` `    ``# in str. ` `    ``for` `i ``in` `range``(``len``(stri)):  ` `        ``if` `a[``ord``(stri[i]) ``-` `ord``(``'a'``)] >``=` `k: ` `            ``print``(stri[i],end``=``'')  ` ` `  `# Driver code ` `k ``=` `2` `findSubsequence(``"geeksforgeeks"``, k) ` ` `  `# This code is contributed by Shubham Rana `

## C#

 `// C# Program to find the subsequence ` `// with each character occurring at  ` `// least k times in string s ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``static` `int` `MAX_CHAR = 26; ` `     `  `    ``// Function to find the subsequence ` `    ``static` `void` `findSubsequence(``string` `str, ``int` `k)  ` `    ``{ ` `         `  `        ``// Taking an extra array to keep ` `        ``// record for character count in s ` `        ``int` `[]a = ``new` `int``[MAX_CHAR]; ` `     `  `        ``// Counting occurrences of all ` `        ``// characters in str[] ` `        ``for` `(``int` `i = 0; i < str.Length; i++) ` `            ``a[str[i] - ``'a'``]++; ` `     `  `        ``// Printing characters with count ` `        ``// >= k in same order as they appear ` `        ``// in str. ` `        ``for` `(``int` `i = 0; i < str.Length; i++) ` `            ``if` `(a[str[i] - ``'a'``] >= k) ` `                ``Console.Write(str[i]); ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main() { ` `         `  `        ``int` `k = 2; ` `        ``findSubsequence(``"geeksforgeeks"``, k); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## PHP

 `= k in same order  ` `    ``// as they appear in str. ` `    ``for` `(``\$i` `= 0; ``\$i` `< ``strlen``(``\$str``); ``\$i``++) ` `        ``if` `(``\$a``[ord(``\$str``[``\$i``]) - ord(``'a'``)] >= ``\$k``) ` `            ``echo` `\$str``[``\$i``]; ` `} ` ` `  `// Driver code ` `\$k` `= 2; ` `findSubsequence(``"geeksforgeeks"``, ``\$k``); ` `     `  `// This code is contributed by Sam007 ` `?> `

Output :

```geeksgeeks
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : Sam007

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.