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

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 <iostream> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP Program to find the ` `// subsequence with each ` `// character occurring at ` `// least k times in string s ` ` ` `// Function to find the subsequence ` `function` `findSubsequence(` `$str` `, ` `$k` `) ` `{ ` ` ` ` ` `// Taking an extra array to keep ` ` ` `// record for character count in s ` ` ` `$a` `= ` `array` `(1024); ` ` ` ` ` `for` `(` `$i` `= 0; ` `$i` `< 26; ` `$i` `++) ` ` ` `$a` `[` `$i` `] = 0; ` ` ` ` ` `// Counting occurrences of all ` ` ` `// characters in str[] ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `strlen` `(` `$str` `); ` `$i` `++) ` ` ` `{ ` ` ` `$temp` `= ord(` `$str` `[` `$i` `]) - ord(` `'a'` `); ` ` ` `$a` `[` `$temp` `] += 1; ` ` ` `} ` ` ` ` ` `// Printing characters with ` ` ` `// count >= 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 ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

geeksgeeks

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:

- Lexicographically largest subsequence such that every character occurs at least k times
- Longest subsequence where every character appears at-least k times
- Find number of times a string occurs as a subsequence in given string
- Queries to print the character that occurs the maximum number of times in a given range
- Longest subsequence with at least one character appearing in every string
- 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
- Number of permutations of a string in which all the occurrences of a given character occurs together
- Longest Subsequence with at least one common digit in every element
- Longest subsequence such that adjacent elements have at least one common digit
- Check if the string contains consecutive letters and each letter occurs exactly once
- Replace every character of string by character whose ASCII value is K times more than it
- Count substrings with each character occurring at most k times
- Length of the longest substring with every character appearing even number of times
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Length of longest subsequence having sum of digits of each element as a Composite Number
- Longest Common Prefix using Character by Character Matching
- Longest sub-string having frequency of each character less than equal to k
- Longest suffix such that occurrence of each character is less than N after deleting atmost K characters
- Count of numbers appearing in the given ranges at-least K times

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.