Longest subsequence where every character appears at | Javascript 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
let MAX_CHARS = 26 ; function longestSubseqWithK ( str , k ) { let n = str . length ; let freq = new Array ( MAX_CHARS ) ; for ( let i = 0 ; i < freq . length ; i ++ ) { freq [ i ] = 0 } for ( let i = 0 ; i < n ; i ++ ) { freq [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } for ( let i = 0 ; i < n ; i ++ ) { if ( freq [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] >= k ) { document . write ( str [ i ] ) ; } } } let str = " " ; let k = 2 ; longestSubseqWithK ( str , k ) ;