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 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; } |
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 = 2findSubsequence("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
<?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 ?> |
Output :
geeksgeeks
Recommended Posts:
- Lexicographically largest subsequence such that every character occurs at least k times
- Longest subsequence where every character appears at-least k times
- Queries to print the character that occurs the maximum number of times in a given range
- Find number of times a string occurs as a subsequence in given string
- 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
- Replace every character of string by character whose ASCII value is K times more than it
- Number of permutations of a string in which all the occurrences of a given character occurs together
- Longest Common Prefix using Character by Character Matching
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Count of substrings which contains a given character K times
- Number of sub-strings that contain the given character exactly 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
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.
Improved By : Sam007



