Rank Words by Frequency
Problem statement
Count the frequency of every word in paragraph and return one entry per distinct word, ordered by decreasing frequency. Break equal-frequency ties by increasing lexicographic word order.
A word is a maximal consecutive run of English letters A-Z or a-z. Every other character separates words. Convert words to lowercase before counting. Each output entry has the form word:count, with an ordinary decimal count and no spaces. Return an empty array when there are no words.
Function
rankWordFrequencies(paragraph: String) → String[]Examples
Example 1
paragraph = "pear PEAR, apple apple APPLE. Kiwi"return = ["apple:3","pear:2","kiwi:1"]Ignoring case, apple appears three times, pear twice, and kiwi once. Sort by those frequencies from largest to smallest.
Example 2
paragraph = "red blue red BLUE"return = ["blue:2","red:2"]Both words occur twice. The frequency tie is resolved lexicographically, so blue comes before red.
Example 3
paragraph = "...123..."return = []There is no run of English letters, so there are no words to rank.
Constraints
0 <= paragraph.length <= 10000- Every character is printable ASCII, a tab, a carriage return, or a line feed.