Viral Content Balancer
The TikTok engineering team is developing a new feature for analyzing viral trends by studying content sequences represented by a string content of length n, consisting of lowercase alphabets. The team has discovered that to make a video go viral, each element (character) in the content sequence should have an equal frequency.
In one operation, a character can be appended or removed from the content string. Your task is to find the minimum number of operations required to balance the frequency of all characters in the content sequence.
🧡 Thanks a gazillion, spike! 🧡
1Example 1
For the content "xzyzxa", the characters 'Z' and 'X' have higher frequencies than others. To balance the frequency of all characters, one possible solution is to append one 'a' and one 'y', resulting in content = "xzyzxyaa". Alternatively, removing one 'x' and one 'z' would also achieve a balanced distribution, with the new content sequence content = "xyza".
Both approaches result in a valid balanced content sequence, and the minimum number of operations required is 2.
2Example 2
3Example 3
Constraints
Limits and guarantees your solution can rely on.
1 <= n <= 10^5The content sequence consists of only lowercase English alphabets