Find the Most Frequent Word
Problem statement
Return the word that occurs most often in paragraph. A word is a maximal consecutive run of English letters A-Z or a-z; every other character separates words.
Count words without regard to case and return the chosen word in lowercase. If several words share the largest count, return the lexicographically smallest one. Return the empty string when the paragraph has no words.
Function
mostFrequentWord(paragraph: String) → StringExamples
Example 1
paragraph = "pear APPLE pear apple apple"return = "apple"After case normalization, apple occurs three times and pear twice. Return apple.
Example 2
paragraph = "red blue RED BLUE"return = "blue"Both words occur twice. The tie rule chooses the lexicographically smaller word, blue.
Example 3
paragraph = "42!?"return = ""There are no English-letter words, so return the empty string.
Constraints
0 <= paragraph.length <= 10000- Every character is printable ASCII, a tab, a carriage return, or a line feed.