Count All Occurrences of Duplicate Words
Problem statement
Count word occurrences that belong to a repeated word group in paragraph. A word occurring c times contributes c when c >= 2, and contributes zero when it occurs once.
A word is a maximal consecutive run of English letters A-Z or a-z; other characters separate words. Compare words without regard to case. Include the first occurrence of every repeated word in the total. Return zero when there are no repeated groups, including when there are no words.
Function
countDuplicateWordOccurrences(paragraph: String) → intExamples
Example 1
paragraph = "This is a nice car, I would like to buy one. But I don't have enough money"return = 2Only i appears more than once. Both of its occurrences count, so return 2.
Example 2
paragraph = "Blue blue BLUE, red red. green"return = 5The repeated groups are blue with three occurrences and red with two. Count all five occurrences, including each group's first occurrence. green occurs once and contributes zero.
Example 3
paragraph = "a b c"return = 0Every word appears exactly once. There are no occurrences belonging to a repeated group.
Constraints
0 <= paragraph.length <= 10000- Every character is printable ASCII, a tab, a carriage return, or a line feed.