Parcel summary
EasyOnline Assessment
Total the weight of parcels per label and list the totals alphabetically.
Each parcel is a pair [label, weight]. Parcels with the same label are combined by adding their weights.
Input: parcels, a list of [label, weight] pairs where label is a string and weight is a positive integer.
Output: a list of [label, totalWeight] pairs, one per distinct label, sorted by label in ascending lexicographic (dictionary) order. A label that is a prefix of another comes first ("a" before "ab"). If there are no parcels, return [].
Example 1
- Input:
- [[["box",3],["bag",2],["box",4]]]
- Output:
- [["bag",2],["box",7]]
- Explanation
- "box" appears twice (3 + 4 = 7) and "bag" once (2). Alphabetically "bag" comes before "box".
Example 2
- Input:
- [[["pear",5],["apple",2],["pear",1],["fig",4],["apple",9]]]
- Output:
- [["apple",11],["fig",4],["pear",6]]
- Explanation
- apple: 2 + 9 = 11, fig: 4, pear: 5 + 1 = 6. Sorted by label, the order is apple, fig, pear (not by weight or first appearance).
Example 3
- Input:
- [[["ab",1],["a",5],["b",2],["a",1]]]
- Output:
- [["a",6],["ab",1],["b",2]]
- Explanation
- "a" totals 5 + 1 = 6, "ab" is 1 and "b" is 2. The shorter label "a" sorts before "ab", which sorts before "b".
Constraints
0 <= parcels.length <= 10,000labelis non-empty and uses lowercase lettersa-zonly1 <= weight <= 1,000