Label inventory
MediumOnline Assessment
Count labels after cleaning them up, and rank them by frequency.
Normalize each string in labels by removing leading and trailing spaces and converting uppercase letters to lowercase (spaces inside a label are kept). Discard any label that is empty after normalizing. Then count how many times each normalized label occurs.
Input: labels, a list of strings.
Output: a list of [label, count] pairs, sorted by count descending. Labels with equal counts are ordered by label in ascending lexicographic order. If nothing remains, return [].
Example 1
- Input:
- [[" Box ","box","BAG"," "]]
- Output:
- [["box",2],["bag",1]]
- Explanation
- " Box " and "box" both normalize to "box" (count 2). "BAG" becomes "bag" (count 1). The label " " is empty after trimming, so it is ignored.
Example 2
- Input:
- [["b","a","b","a"]]
- Output:
- [["a",2],["b",2]]
- Explanation
- "a" and "b" each occur twice. The counts tie, so they are ordered alphabetically: "a" first.
Example 3
- Input:
- [[" Tea","coffee ","TEA","Coffee","tea"," ","water"]]
- Output:
- [["tea",3],["coffee",2],["water",1]]
- Explanation
- tea appears 3 times, coffee 2 times, water once; the blank entry is dropped. Sorted by count descending: tea, coffee, water.
Constraints
0 <= labels.length <= 10,000- Each label has at most 100 characters, all ASCII letters or spaces