# Two ways of Python Solution, easy to understand.

• The basic idea is

1. Count the char;
2. Sort the counts of all chars;
3. Construct return String from the char with highest counts
Although it is really easy to use collections.Counter(Solution1), I still re-code it using normal operations(Solution2). Cos in most interviews, I guess you are supposed to write the code in the second way.
Happy coding!!!

This is a solution based on collections.Counter

``````class Solution(object):
def frequencySort(self, s):
import collections
if not s:
return ""
count_s = collections.Counter(s)
counter = count_s.most_common()
rs = ''
for i in counter:
rs += i[0] * i[1]
return rs
``````

This is a solution that doesn't use collections.Counter

``````class Solution(object):
def frequencySort(self, s):
import operator
if not s:
return ""
counter = {}; rs = ''
for i in s:
counter[i] = 1 if i not in counter else counter[i]+1
sorted_counter = sorted(counter.items(), key=operator.itemgetter(1))
sorted_counter.reverse()
for i in sorted_counter:
rs += i[0] * i[1]
return rs
``````

• @weih1214 Very readable, good solution and thanks for sharing!

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.