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