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
def subsequence ( s , t , n , k ) : NEW_LINE INDENT last = 0 NEW_LINE cnt = 0 NEW_LINE new_last = 0 NEW_LINE size = 0 NEW_LINE string = ' zyxwvutsrqponmlkjihgfedcba ' NEW_LINE DEDENT ' NEW_LINE INDENT for ch in string : NEW_LINE INDENT cnt = 0 NEW_LINE for i in range ( last , n ) : NEW_LINE INDENT if s [ i ] == ch : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT if cnt >= k : NEW_LINE INDENT for i in range ( last , n ) : NEW_LINE INDENT if s [ i ] == ch : NEW_LINE INDENT t [ size ] = ch NEW_LINE new_last = i NEW_LINE size += 1 NEW_LINE DEDENT DEDENT last = new_last NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = [ ' b ' , ' a ' , ' n ' , ' a ' , ' n ' , ' a ' ] NEW_LINE n = len ( s ) NEW_LINE k = 2 NEW_LINE t = [ ' ' ] * n NEW_LINE subsequence ( s , t , n - 1 , k ) NEW_LINE t = ' ' . join ( t ) NEW_LINE print ( t ) NEW_LINE DEDENT