MapReduce processes records in two conceptual phases. The map phase emits intermediate key-value pairs, and the reduce phase combines all values belonging to the same key.
Design a MapReduceWordCounter class:
MapReduceWordCounter() creates a stateless counter.String mostFrequent(String[] lines) returns the most frequent word across all lines.
For every line, split its words on single spaces and convert each word to lowercase. Conceptually, the map phase emits (word, 1) for every occurrence, and the reduce phase sums the values for each word.
Return the lowercase word with the highest total count. If multiple words share the highest count, return the alphabetically smallest one.
Example 1:
Input:
Output:
Explanation: "the" appears 3 times, "cat" appears 2 times, and every other word appears once.
Example 2:
Input:
Output:
Explanation: Lowercasing produces two occurrences each of "alpha" and "beta". The counts tie, so the alphabetically smaller word, "alpha", is returned.
Constraints
1 <= lines.length <= 2001 <= lines[i].length <= 200- Every line contains one or more words separated by single spaces.
- Every word contains only English letters.
- The total number of words across all lines is at most
10^4. - Alphabetical comparison uses the lowercase words.
- At most
100 calls are made to mostFrequent.