Longest subsequence where each character occurs at least k times Last Updated : 20 Mar, 2025 Comments Improve Suggest changes Like Article Like Report 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 = 2Output geeksgeeksExplanation 'g', 'e', 'k', and 's' appear twice or more, so the output is "geeksgeeks"Input s = "baaabaacba" k = 3Output baaabaabaExplanation Characters 'b' and 'a' appear at least 3 times, so the result is "baaabaaba".[Naive Approach] Generate All Subsequences - Exponential timeWe can solve the problem by generating all subsequences of the string. For each subsequence, we check if every character appears at least k times. Among all valid subsequences, we keep track of the longest one. [Efficient Approach] Using Frequency Counting - O(n) time and O(1) spaceWe use a counter array to store the frequency of each character in the string. Then, iterate through the string, and for each character, check if its count is greater than or equal to k. If it is, include that character in the result. C++ #include <iostream> using namespace std; #define MAX_CHAR 26 // Function to find the subsequence string findSubsequence(string &str, int k) { int count[MAX_CHAR] = { 0 }; // Counting occurrences of all characters for (char c : str) count[c - 'a']++; // Storing characters with count >= k string res; for (char c : str) if (count[c - 'a'] >= k) res += c; return res; } int main() { string str = "geeksforgeeks"; int k = 2; cout << findSubsequence(str, k); return 0; } Java // Importing necessary libraries import java.util.HashMap; import java.util.Map; public class Main { // Function to find the subsequence public static String findSubsequence(String str, int k) { int[] count = new int[26]; // Counting occurrences of all characters for (char c : str.toCharArray()) count[c - 'a']++; // Storing characters with count >= k StringBuilder res = new StringBuilder(); for (char c : str.toCharArray()) if (count[c - 'a'] >= k) res.append(c); return res.toString(); } public static void main(String[] args) { String str = "geeksforgeeks"; int k = 2; System.out.println(findSubsequence(str, k)); } } Python # Function to find the subsequence def find_subsequence(s, k): count = [0] * 26 # Counting occurrences of all characters for c in s: count[ord(c) - ord('a')] += 1 # Storing characters with count >= k res = '' for c in s: if count[ord(c) - ord('a')] >= k: res += c return res # Main function if __name__ == '__main__': str = 'geeksforgeeks' k = 2 print(find_subsequence(str, k)) C# // Function to find the subsequence using System; using System.Collections.Generic; class Program { public static string FindSubsequence(string str, int k) { int[] count = new int[26]; // Counting occurrences of all characters foreach (char c in str) count[c - 'a']++; // Storing characters with count >= k string res = ""; foreach (char c in str) if (count[c - 'a'] >= k) res += c; return res; } static void Main() { string str = "geeksforgeeks"; int k = 2; Console.WriteLine(FindSubsequence(str, k)); } } JavaScript // Function to find the subsequence function findSubsequence(str, k) { const count = new Array(26).fill(0); // Counting occurrences of all characters for (let c of str) count[c.charCodeAt(0) - 'a'.charCodeAt(0)]++; // Storing characters with count >= k let res = ''; for (let c of str) if (count[c.charCodeAt(0) - 'a'.charCodeAt(0)] >= k) res += c; return res; } // Main function const str = 'geeksforgeeks'; const k = 2; console.log(findSubsequence(str, k)); Outputgeeksgeeks Comment More infoAdvertise with us Next Article Lexicographically largest subsequence such that every character occurs at least k times S Sakshi_Tiwari Follow Improve Article Tags : Misc Strings DSA subsequence frequency-counting +1 More Practice Tags : MiscStrings Similar Reads 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 = "geeksforgeeks", k = 2Output: geeksgeeksExplanation: Every character in the output subsequence appears at-least 2 times. Input : str = "aabbaabacabb", k = 5O 12 min read Longest substring where all the characters appear at least K times | Set 3 Given a string str and an integer K, the task is to find the length of the longest substring S such that every character in S appears at least K times. Examples: Input: str = âaabbbaâ, K = 3Output: 6Explanation: In substring "aabbba", each character repeats at least k times and its length is 6. Inpu 12 min read Count substrings with each character occurring at most k times Given a string S. Count number of substrings in which each character occurs at most k times. Assume that the string consists of only lowercase English alphabets. Examples: Input : S = ab k = 1 Output : 3 All the substrings a, b, ab have individual character count less than 1. Input : S = aaabb k = 2 15+ min read Longest subsequence with at least one character appearing in every string Given a string array arr[], the task is to find the longest subsequence of the array with at least one character appearing in all the strings. Note that all the strings contain only lowercase English alphabets.Examples: Input: str = {"ab", "bc", "de"} Output: 2 {"ab", "bc"} is the required sub-seque 6 min read Lexicographically largest subsequence such that every character occurs at least k times Given a string s and an integer k, the task is to find lexicographically largest subsequence of S, say T, such that every character in T must occur at least k times.Examples: Input : s = "banana", k = 2.Output : "nn"Explanation: Possible subsequence where each character exists at least 2 times are:F 6 min read Longest Subsequence with difference between characters equals to K Given a string S consisting of lowercase letters. Find the longest subsequence of S such that the difference between the maximum and minimum occurring characters in the subsequence is exactly K. Examples: Input: S = 'abcdeg' and K = 4Output: abcde Input: S = 'daaaabbbadddddeeee', K = 1Output: dddddd 9 min read Like