Minimize steps to form string S from any random string of length K using a fixed length subsequences | ''Function to find the minimum number of string required to generate the original string ; '' Stores the frequency of each  character of String S ; '' Stores the frequency of each  character of String S ; '' Count unique characters in S ; '' If unique characters is greater  then N, then return -1 ; '' Otherwise ; '' Perform Binary Search ; '' Find the value of mid ; '' Iterate over the range  [0, 26] ; '' If the amount[i] is  greater than 0 ; '' Update the ranges ; '' Find the resultant string ; Generate the subsequence ; ' ' ▁ If ▁ the ▁ length ▁ of ▁ resultant ▁ ▁ string ▁ is ▁ less ▁ than ▁ N ▁ than ▁ ▁ add ▁ a ▁ character ▁ ' a '  ; '' Print the string ; ''Driver code
def findString ( S , N ) : NEW_LINE INDENT amounts = [ 0 ] * 26 NEW_LINE for i in range ( len ( S ) ) : NEW_LINE INDENT amounts [ ord ( S [ i ] ) - 97 ] += 1 NEW_LINE DEDENT count = 0 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if amounts [ i ] > 0 : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT if count > N : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT ans = " " NEW_LINE high = 100001 NEW_LINE low = 0 NEW_LINE while ( high - low ) > 1 : NEW_LINE INDENT total = 0 NEW_LINE mid = ( high + low ) // 2 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if amounts [ i ] > 0 : NEW_LINE INDENT total += ( amounts [ i ] - 1 ) // mid + 1 NEW_LINE DEDENT DEDENT if total <= N : NEW_LINE INDENT high = mid NEW_LINE DEDENT else : NEW_LINE INDENT low = mid NEW_LINE DEDENT DEDENT print ( high , end = " ▁ " ) NEW_LINE total = 0 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if amounts [ i ] > 0 : NEW_LINE INDENT total += ( amounts [ i ] - 1 ) // high + 1 NEW_LINE for j in range ( ( amounts [ i ] - 1 ) // high + 1 ) : NEW_LINE INDENT ans += chr ( i + 97 ) NEW_LINE DEDENT DEDENT DEDENT for i in range ( total , N ) : NEW_LINE INDENT ans += ' a ' NEW_LINE DEDENT ans = ans [ : : - 1 ] NEW_LINE print ( ans ) NEW_LINE DEDENT DEDENT S = " toffee " NEW_LINE K = 4 NEW_LINE findString ( S , K ) NEW_LINE