Index in freq [ ] to segregate the string
pos = ord ( Q [ 0 ] ) - ord ( ' a ' ) NEW_LINE for i in range ( pos + 1 ) : NEW_LINE INDENT while freq [ i ] > 0 : NEW_LINE INDENT sb += chr ( ord ( ' a ' ) + i ) NEW_LINE freq [ i ] -= 1 NEW_LINE DEDENT DEDENT