FastPrepFastPrep
Problem Brief

Viral Content Balancer

FULLTIMEOA
See Tiktok online assessment and hiring insights

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

Input
content = "xzyzxa"
Output
2
Explanation

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

Input
content = "ababc"
Output
1
Explanation
In the given content sequence, the frequency of 'a' and 'b' is higher than that of 'c'. To balance the distribution, one operation is needed. Removing the occurence of 'c'would result in the remaining characters having equal frequencies, making the content sequence content 'abab'. Thus, the min num of operations required is 1.

3Example 3

Input
content = "aabbc"
Output
1
Explanation
The min num of operations required is 1.

Constraints

Limits and guarantees your solution can rely on.

  • 1 <= n <= 10^5
  • The content sequence consists of only lowercase English alphabets
  • public int balanceContentFrequency(String content) {
      // write your code here
    }
    
    Input

    content

    "xzyzxa"

    Output

    2

    Sign in to submit your solution.