Given a string s
, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string.
Return the sorted string. If there are multiple answers, return any of them.
Example 1:
Input: s = "tree" Output: "eert" Explanation: 'e' appears twice while 'r' and 't' both appear once. So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.
Example 2:
Input: s = "cccaaa" Output: "aaaccc" Explanation: Both 'c' and 'a' appear three times, so both "cccaaa" and "aaaccc" are valid answers. Note that "cacaca" is incorrect, as the same characters must be together.
Example 3:
Input: s = "Aabb" Output: "bbAa" Explanation: "bbaA" is also a valid answer, but "Aabb" is incorrect. Note that 'A' and 'a' are treated as two different characters.
Constraints:
1 <= s.length <= 5 * 105
s
consists of uppercase and lowercase English letters and digits.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to sorting characters by frequency involves counting how often each character appears and then trying every possible arrangement of the characters. We generate all possible strings and pick the one that matches the frequency count and is sorted correctly. This is the most basic and computationally expensive way to solve the problem.
Here's how the algorithm would work step-by-step:
def sort_characters_by_frequency_brute_force(input_string):
character_counts = {}
for character in input_string:
character_counts[character] = character_counts.get(character, 0) + 1
unique_characters = list(character_counts.keys())
all_possible_strings = []
def generate_strings(current_string, remaining_counts):
if not remaining_counts:
all_possible_strings.append(current_string)
return
for character in unique_characters:
if remaining_counts.get(character, 0) > 0:
new_counts = remaining_counts.copy()
new_counts[character] -= 1
if new_counts[character] == 0:
del new_counts[character]
generate_strings(current_string + character, new_counts)
generate_strings("", character_counts)
possible_solutions = []
# We must check that the generated string is correctly sorted by frequency
for possible_string in all_possible_strings:
is_sorted_correctly = True
for i in range(len(possible_string) - 1):
first_character = possible_string[i]
second_character = possible_string[i+1]
if character_counts[first_character] < character_counts[second_character]:
is_sorted_correctly = False
break
if is_sorted_correctly:
possible_solutions.append(possible_string)
# Any string that is sorted correctly is a valid response
if possible_solutions:
return possible_solutions[0]
else:
return ""
To sort characters by frequency, we first count how often each character appears. Then, we use this information to arrange the characters, placing the most frequent ones first.
Here's how the algorithm would work step-by-step:
def sort_characters_by_frequency(input_string):
character_frequencies = {}
for char in input_string:
character_frequencies[char] = character_frequencies.get(char, 0) + 1
# Sort characters by frequency, most frequent first.
sorted_characters = sorted(character_frequencies.items(), key=lambda item: item[1], reverse=True)
result_string = ''
# Build the result string based on the sorted character frequencies.
for char, frequency in sorted_characters:
result_string += char * frequency
return result_string
Case | How to Handle |
---|---|
Null or empty input string | Return an empty string or throw an IllegalArgumentException, depending on requirements, handling invalid inputs gracefully. |
String with only one distinct character | The algorithm should correctly return the input string, as it's already sorted by frequency. |
String with maximum possible length (considering memory constraints) | Ensure the frequency map and output string buffer can accommodate the maximum length without memory issues or integer overflows. |
String with all identical characters | The algorithm should correctly return the string, repeating the character n times where n is the length. |
String with a wide range of ASCII/Unicode characters | Ensure the frequency map can handle the entire character set without collisions or out-of-bounds errors. |
String with mixed case characters (e.g., 'A' and 'a') | Consider whether case sensitivity is required; if not, normalize the case before counting frequencies. |
Multiple characters with the same frequency | The algorithm should sort them according to the original input's order, or document that order is undefined. |
String containing whitespace characters | Whitespace characters should be treated like any other character, counted and sorted accordingly. |