Count Repetitions for Each Word
Problem statement
Return a separate repetition count for every distinct word in paragraph. A word is a maximal consecutive run of English letters A-Z or a-z; all other characters separate words. Convert words to lowercase before comparing them.
For a word appearing c times, its repetition count is c - 1: the first occurrence is not a repetition. Include words with zero repetitions. Return strings word:repetitions in increasing lexicographic word order, using ordinary decimal counts and no spaces. Return an empty array when there are no words.
Function
wordRepetitionCounts(paragraph: String) → String[]Examples
Example 1
paragraph = "Blue blue BLUE, red red. green"return = ["blue:2","green:0","red:1"]The first occurrence of each word contributes zero. The two later blue occurrences contribute 2, the later red contributes 1, and the single green contributes 0. Return an entry for every distinct word in lexical order.
Example 2
paragraph = "zebra APPLE zebra apple"return = ["apple:1","zebra:1"]Case-insensitive counting gives two occurrences of each word. Each word therefore has one occurrence after its first. Sort the two entries by word.
Example 3
paragraph = "123,!?"return = []There are no English-letter words, so the result is empty.
Constraints
0 <= paragraph.length <= 10000- Every character is printable ASCII, a tab, a carriage return, or a line feed.