1224. Echoed Words Tally
An editor wants to flag words that a writer repeats. In text, a word is a maximal run of ASCII letters (a-z or A-Z); every other character, such as a digit, space or punctuation mark, only separates words. Words are compared after converting them to lower case.
Return a map from each word that occurs at least twice in text to the number of times it occurs. Words that appear only once are left out, and the result is an empty map if nothing repeats. The order of the map entries does not matter.
Aim for O(n) time and O(n) space with a hash map of counts.
Example 1
- Input:
- text = "The cat saw the cat; THE end."
- Output:
- {"cat":2,"the":3}
- Explanation:
The words are the, cat, saw, the, cat, the, end, so "the" occurs 3 times and "cat" 2 times.
Example 2
- Input:
- text = "a-b c"
- Output:
- {}
- Explanation:
Every word appears once, so the map is empty.
Example 3
- Input:
- text = "Go go GO! go?go"
- Output:
- {"go":5}
- Explanation:
Letters split into five "go" words regardless of case and punctuation, so the count is 5.
Constraints
1 ≤ text.length ≤ 2000
text contains printable ASCII characters only.
At most 200 distinct words.
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(n)
- Space
- O(n)