Skip to content
Related Articles
Longest subsequence where each character occurs at least k times
• Difficulty Level : Easy
• Last Updated : 06 May, 2021

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

## Javascript

 ``

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes

My Personal Notes arrow_drop_up