Problem
Welcome to Tailoria, where the ancient tailor relies on your help to organize a collection of enchanted ribbons, each defined by a unique length and color code. Your task is to group ribbons by their color and sort them by length within each group using an efficient two-pointer technique. Begin by reading the ribbon data, which consists of a list of ribbons, each with a color and length attribute. Your goal is to output the ribbons grouped by color, with each group sorted in ascending order of length. For example, given ribbons of colors red, blue, and red with lengths 3, 1, and 2 respectively, your output should group the reds together and sort them, resulting in: blue (1), red (2), red (3). Consider edge cases such as ribbons with the same length and color or only one ribbon.
- Approach: Start by segregating ribbons based on color using a dictionary or hash map.
- Algorithm: Utilize a two-pointer technique to efficiently sort ribbons within each color group.
- Edge Cases: Consider scenarios where all ribbons are the same color or have the same length.
- Optimization: Think about how you can minimize the number of comparisons needed by leveraging the properties of the two-pointer technique.
Input format
The input consists of multiple lines. The first line contains an integer n (1 <= n <= 1000) — the number of ribbons. The following n lines each contain a string s and an integer l separated by a space, where s is the color code of the ribbon and l (1 <= l <= 100) is the length of the ribbon.
Output format
Output the ribbons grouped by color code in alphabetical order, with each group's ribbons sorted by length. Each ribbon should be on a new line in the format 's l'.
Constraints
1 <= n <= 1000, 1 <= l <= 100, Color code is a string of lowercase letters.
Sample input
5 red 5 blue 3 red 4 blue 6 green 2
Sample output
blue 3 blue 6 green 2 red 4 red 5