Problem · String
Count Repeated Words
Learn this problemProblem statement
Given a paragraph, count how many distinct words occur at least twice. Matching is case-insensitive, so differently capitalized forms of the same word belong to one group.
The only punctuation marks in the paragraph are periods and commas, and they are used correctly. Punctuation is not part of a word. A word that appears two or more times contributes exactly 1 to the answer, regardless of how many total occurrences it has.
Function
countRepeatedWords(paragraph: String) → intExamples
Example 1
paragraph = "Sun rises, and sun sets."return = 1Sun and sun are the same word after case normalization, so sun contributes one.
Example 2
paragraph = "Go, go. GO, stay."return = 1go appears three times but still contributes only one distinct repeated word.
Example 3
paragraph = "Hello, world."return = 0Neither word repeats.
Constraints
1 <= paragraph.length <= 200000- The paragraph contains English letters, spaces, periods, and commas only.
- Periods and commas are used correctly and separate from the words they punctuate.
- The paragraph contains at least one word.