Groups of Lists

Lab Java Intermediate 20 points

Read n words and group them by length into a List<List<String>> where index k holds words of length k. Print each non-empty group as k: [words].

Input

n, then n words (length ≤ 10).

Output

One line per length.

Example

Input

6
hi to java code a list

Output

1: [a]
2: [hi, to]
4: [java, code, list]
Sample input available Sample output available
Editor
Java

Hints

Create the inner lists before adding to them.

Solution explanation

Collections can be nested just like arrays.

Show solution
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        List<List<String>> groups = new ArrayList<List<String>>();
        for (int k = 0; k <= 10; k++) {
            groups.add(new ArrayList<String>());
        }
        for (int i = 0; i < n; i++) {
            String w = sc.next();
            groups.get(w.length()).add(w);
        }
        for (int k = 0; k <= 10; k++) {
            if (!groups.get(k).isEmpty()) {
                System.out.println(k + ": " + groups.get(k));
            }
        }
    }
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD