if sum == n means this char will provide its greatest permutation as nth permutation
i = MAX_CHAR - 1 NEW_LINE while k < length and i >= 0 : NEW_LINE INDENT if freq [ i ] : NEW_LINE INDENT out [ k ] = chr ( i + ord ( ' a ' ) ) NEW_LINE freq [ i ] -= 1 NEW_LINE i += 1 NEW_LINE k += 1 NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT