First-letter statistics
Collections and grouping
Count words by their lower-case first letter. Input strings are either empty or contain only ASCII letters A–Z/a–z. Ignore empty strings and return a Map whose keys iterate in ascending order.
This is a Kotlin/JVM exercise: the reference uses the JVM toSortedMap API. The alphabet restriction is intentional; Char ordering is not linguistic ordering of arbitrary Unicode text.
Check:
- The provided main example →
{a=2, c=1, k=2}. - An empty list or only empty strings → an empty Map.
listOf("Zoo", "apple", "APPLE", "")→{a=2, z=1}.- Count each word once; the rest of its spelling does not affect its group.
This saved draft exceeds the editor limit. Download it before resetting; the original is preserved.
Filter empty strings before first(). Grouping with eachCount counts each group without building a list of its elements. groupingBy, eachCount.
LowercaseChar performs a one-Char-to-one-Char mapping, not full Unicode case folding. The ASCII input contract makes this sufficient. lowercaseChar.
ToSortedMap is a JVM API using the natural key order. Normalized ASCII a–z therefore iterate as required. CommonMain or another alphabet needs an explicit ordering contract and suitable implementation. toSortedMap.
The reference and its example/boundary cases were executed with Kotlin/JVM 2.4.20, JDK 17.