Problem · String

Viral Content Balancer

Learn this problem
MediumTiktok logoTiktokFULLTIMEOA
See Tiktok hiring insights

Problem statement

The TikTok engineering team is developing a new feature for analyzing viral trends by studying content sequences represented by a string of content of length n consisting of lowercase letters. The team has discovered that to make a video go viral, each character (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.

Function

balanceContent(content: String) → int

Complete the function balanceContent in the editor.

balanceContent has the following parameter:

  • string content: the original content sequence.

Returns

int: the minimum number of operations to balance the content sequence.

Examples

Example 1

content = "xzyzxa"return = 2
For the content "xzyzxa", the characters 'z' and 'x' have higher frequencies than others. To balance the frequency of characters, one possible solution is to append one 'a' and one 'y', resulting in content = "xzyzxaya". Alternatively, remove one 'x' and one 'z' would also give 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.

More Tiktok problems

drafts saved locally
public int balanceContent(String content) {
  // write your code here
}
content"xzyzxa"
expected2
checking account