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

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

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

Implementation:

CPP

 `// CPP Program to find the subsequence` `// with each character occurring at least` `// k times in string str` `#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 < str.size(); 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`

Time Complexity: O(n), where n is the size of the given string.
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next