Lexicographically largest subsequence such that every character occurs at least k times | Find lexicographically largest subsequence of s [ 0. . n - 1 ] such that every character appears at least k times . The result is filled in t [ ] ; Starting from largest charter ' z ' to ' a ' ; Counting the frequency of the character ; If frequency is greater than k ; From the last point we leave ; check if string contain ch ; If yes , append to output string ; Update the last point . ; Driver code
function subsequence ( s , t , n , k ) { var last = 0 , cnt = 0 , new_last = 0 , size = 0 ; for ( var ch = ' ' . charCodeAt ( 0 ) ; ch >= ' ' . charCodeAt ( 0 ) ; ch -- ) { cnt = 0 ; for ( var i = last ; i < n ; i ++ ) { if ( s [ i ] . charCodeAt ( 0 ) == ch ) cnt ++ ; } if ( cnt >= k ) { for ( var i = last ; i < n ; i ++ ) { if ( s [ i ] . charCodeAt ( 0 ) == ch ) { t [ size ++ ] = String . fromCharCode ( ch ) ; new_last = i ; } } last = new_last ; } } } var s = " " ; var n = s . length ; var k = 2 ; var t = Array ( n ) ; subsequence ( s , t , n - 1 , k ) ; document . write ( t . join ( ' ' ) ) ;