```
result={}
max1=0
min1=len(s)
for i in xrange(len(s)):
if s[i] in result:
result[s[i]]+=1
if result[s[i]]>max1:
max1=result[s[i]]
if result[s[i]]<min1:
min1=result[s[i]]
else:
result[s[i]]=1
if max1<1:
max1=1
if min1>1:
min1=1
rlist=[]
for i in xrange(max1):
rlist.append([])
for key in result.keys():
rlist[result[key]-1].append(key)
finalstring=''
for i in xrange(len(rlist)-1,-1,-1):
if len(rlist[i])!=0:
for w in rlist[i]:
for j in xrange(i+1):
finalstring=finalstring+w
return finalstring
```