FastPrepCount Repeated Words
Problem · String

Count Repeated Words

Learn this problem
EasyDatadog logoDatadogINTERNPHONE SCREEN

Problem 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) → int

Examples

Example 1

paragraph = "Sun rises, and sun sets."return = 1

Sun and sun are the same word after case normalization, so sun contributes one.

Example 2

paragraph = "Go, go. GO, stay."return = 1

go appears three times but still contributes only one distinct repeated word.

Example 3

paragraph = "Hello, world."return = 0

Neither 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.

More Datadog problems

drafts saved locally
public int countRepeatedWords(String paragraph) {
  // Write your code here.
}
paragraph"Sun rises, and sun sets."
expected1
checking account