#K32937. Lyric Frequency

    ID: 24976 Type: Default 1000ms 256MiB

Lyric Frequency

Lyric Frequency

Given the lyrics of a song as a single line of text, calculate the frequency of each unique word. Words are separated by whitespace and the lyrics may be empty. The output must be a JSON array of objects, where each object has two keys: word and frequency. The list must be sorted primarily by frequency in descending order. If two words have the same frequency, they should appear in the order of their first occurrence in the lyrics.

In other words, if the input lyrics are given by the string \(S\), you should:

  • Split \(S\) into words.
  • Count occurrences of each word.
  • Sort the words by their frequency (highest first). For words with the same frequency, maintain the order in which they first appear in \(S\).
  • Output the result as a JSON array where each element is of the form {"word": word, "frequency": count}.

inputFormat

The input consists of a single line, representing the lyrics of a song. The line may be empty.

outputFormat

The output is a JSON array of objects. Each object has exactly two properties: "word" (a string) and "frequency" (an integer). The array must be printed to stdout.

For example, given the input "baby shark do do do do do do baby shark", the output should be:

[{"word": "do", "frequency": 6}, {"word": "baby", "frequency": 2}, {"word": "shark", "frequency": 2}]
## sample
baby shark do do do do do do baby shark
[{"word": "do", "frequency": 6}, {"word": "baby", "frequency": 2}, {"word": "shark", "frequency": 2}]