Word Frequency Count
core
Implement vector<pair<string,int>> freq_count(const vector<string>& words) (words is a list of strings in Python; return a list of (str, int) tuples; in JavaScript, an array of strings in, an array of [string, number] pairs out) that counts how many times each distinct word appears in words, using a hash map for the counting. Return the (word, count) pairs sorted by count in descending order; when two or more words share the same count, break the tie by sorting those words in ascending alphabetical order. Every word consists of lowercase letters only.
Example: freq_count({"a", "b", "a", "c", "b", "a"}) returns {{"a", 3}, {"b", 2}, {"c", 1}}: a appears 3 times, b twice, c once, and no two words share a count here so the alphabetical tiebreak never triggers.
Input format: each test in tests/*.in is laid out as:
- Line 1:
n, the number of words. - Line 2: all
nwords on one line, space-separated.
Test 03.in reads:
5
b b a a c
This is five words on a single line; b and a each appear twice, c once.
Where you'll use it:
✦ Solution & editorial unlock with the pass.