diff --git a/Sort Characters By Frequency.py b/Sort Characters By Frequency.py new file mode 100644 index 0000000..7f026bc --- /dev/null +++ b/Sort Characters By Frequency.py @@ -0,0 +1,24 @@ +class Solution: + def frequencySort(self, s): + freq = {} + for char in s: + if char in freq: + freq[char] += 1 + else: + freq[char] = 1 + + sorted_chars = sorted(freq, key=freq.get, reverse=True) + + sorted_str = "" + for char in sorted_chars: + sorted_str += char * freq[char] + + return sorted_str + +# Input +s = input("Enter a string: ") + +# Output +solution = Solution() +result = solution.frequencySort(s) +print("Sorted string by character frequency:", result)