# Longest subsequence where every character appears at-least k times

Given a string and a number k, find the longest subsequence of a string where every character appears at-least k times.

Examples:

Input : str = "geeksforgeek" k = 2 Output : geeskgeeks Every character in the output subsequence appears at-least 2 times. Input : str = "aabbaabacabb" k = 5 Output : aabbaabaabb

** Method 1 (Brute force)**

We generate all subsequences. For every subsequence count distinct characters in it and find the longest subsequence where every character appears at-least k times.

** Method 2 (Efficient way)**

1. Find the frequency of the string and store it in an integer array of size 26 representing the alphabets.

2. After finding the frequency iterate the string character by character and if the frequency of that character is greater than or equal to the required number of repetitions then print that character then and there only.

## C++

`// C++ program to Find longest subsequence where ` `// every character appears at-least k times ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `const` `int` `MAX_CHARS = 26; ` ` ` `void` `longestSubseqWithK(string str, ` `int` `k) ` `{ ` ` ` `int` `n = str.size(); ` ` ` ` ` `// Count frequencies of all characters ` ` ` `int` `freq[MAX_CHARS] = {0}; ` ` ` `for` `(` `int` `i = 0 ; i < n; i++) ` ` ` `freq[str[i] - ` `'a'` `]++; ` ` ` ` ` `// Traverse given string again and print ` ` ` `// all those characters whose frequency ` ` ` `// is more than or equal to k. ` ` ` `for` `(` `int` `i = 0 ; i < n ; i++) ` ` ` `if` `(freq[str[i] - ` `'a'` `] >= k) ` ` ` `cout << str[i]; ` `} ` ` ` `// Driver code ` `int` `main() { ` ` ` `string str = ` `"geeksforgeeks"` `; ` ` ` `int` `k = 2; ` ` ` `longestSubseqWithK(str, k); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to Find longest subsequence where ` `// every character appears at-least k times ` ` ` `class` `GFG { ` ` ` ` ` `static` `final` `int` `MAX_CHARS = ` `26` `; ` ` ` ` ` `static` `void` `longestSubseqWithK(String str, ` `int` `k) { ` ` ` `int` `n = str.length(); ` ` ` ` ` `// Count frequencies of all characters ` ` ` `int` `freq[] = ` `new` `int` `[MAX_CHARS]; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` `freq[str.charAt(i) - ` `'a'` `]++; ` ` ` `} ` ` ` ` ` `// Traverse given string again and print ` ` ` `// all those characters whose frequency ` ` ` `// is more than or equal to k. ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` `if` `(freq[str.charAt(i) - ` `'a'` `] >= k) { ` ` ` `System.out.print(str.charAt(i)); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `// Driver code ` ` ` `static` `public` `void` `main(String[] args) { ` ` ` `String str = ` `"geeksforgeeks"` `; ` ` ` `int` `k = ` `2` `; ` ` ` `longestSubseqWithK(str, k); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 program to Find longest subsequence where ` `# every character appears at-least k times ` ` ` `MAX_CHARS ` `=` `26` ` ` `def` `longestSubseqWithK(` `str` `, k): ` ` ` ` ` `n ` `=` `len` `(` `str` `) ` ` ` ` ` `# Count frequencies of all characters ` ` ` `freq ` `=` `[` `0` `]` `*` `MAX_CHARS ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `freq[` `ord` `(` `str` `[i]) ` `-` `ord` `(` `'a'` `)]` `+` `=` `1` ` ` ` ` `# Traverse given string again and print ` ` ` `# all those characters whose frequency ` ` ` `# is more than or equal to k. ` ` ` `for` `i ` `in` `range` `(n ): ` ` ` `if` `(freq[` `ord` `(` `str` `[i]) ` `-` `ord` `(` `'a'` `)] >` `=` `k): ` ` ` `print` `(` `str` `[i],end` `=` `"") ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `str` `=` `"geeksforgeeks"` ` ` `k ` `=` `2` ` ` `longestSubseqWithK(` `str` `, k) ` |

*chevron_right*

*filter_none*

## C#

` ` `// C# program to Find longest subsequence where ` `// every character appears at-least k times ` `using` `System; ` `public` `class` `GFG { ` ` ` ` ` `static` `readonly` `int` `MAX_CHARS = 26; ` ` ` ` ` `static` `void` `longestSubseqWithK(String str, ` `int` `k) { ` ` ` `int` `n = str.Length; ` ` ` ` ` `// Count frequencies of all characters ` ` ` `int` `[]freq = ` `new` `int` `[MAX_CHARS]; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `freq[str[i]- ` `'a'` `]++; ` ` ` `} ` ` ` ` ` `// Traverse given string again and print ` ` ` `// all those characters whose frequency ` ` ` `// is more than or equal to k. ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `if` `(freq[str[i] - ` `'a'` `] >= k) { ` ` ` `Console.Write(str[i]); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `// Driver code ` ` ` `static` `public` `void` `Main() { ` ` ` `String str = ` `"geeksforgeeks"` `; ` ` ` `int` `k = 2; ` ` ` `longestSubseqWithK(str, k); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

**Output:**

geeksgeeks

This code has a time complexity of O(n) where n is the size of the string.

This article is contributed by **Mohak Agrawal**. 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.

## Recommended Posts:

- Longest subsequence where each character occurs at least k times
- Check if max occurring character of one string appears same no. of times in other
- Lexicographically largest subsequence such that every character occurs at least k times
- Longest subsequence with at least one character appearing in every string
- Minimum K such that every substring of length atleast K contains a character c
- Remove characters from string that appears strictly less than K times
- Replace every character of string by character whose ASCII value is K times more than it
- Longest Common Prefix using Character by Character Matching
- Count of substrings which contains a given character K times
- Number of sub-strings that contain the given character exactly k times
- Count substrings with each character occurring at most k times
- Print the string after the specified character has occurred given no. of times
- Encrypt a string by repeating i-th character i times
- Decrypt a string encrypted by repeating i-th character i times
- Longest Palindromic Subsequence | DP-12