How to group words in a string by the first N letters in Java

1 Answer

0 votes
import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class GroupWords {

    public static Map<String, List<String>> groupByFirstNLetters(String s, int n) {
        Map<String, List<String>> groups = new HashMap<>();

        Pattern pattern = Pattern.compile("[A-Za-z]+");
        Matcher matcher = pattern.matcher(s.toLowerCase());

        while (matcher.find()) {
            String word = matcher.group();
            if (word.length() >= n) {
                String prefix = word.substring(0, n);
                groups.computeIfAbsent(prefix, k -> new ArrayList<>()).add(word);
            }
        }

        return groups;
    }

    public static void main(String[] args) {
        String s = "The lowly inhabitants of the lowland were surprised to see "
                 + "the lower branches of the trees.";

        Map<String, List<String>> groups = groupByFirstNLetters(s, 3);

        // Print version 1
        for (var entry : groups.entrySet()) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }

        System.out.println();

        // Print version 2
        for (var entry : groups.entrySet()) {
            System.out.println(entry.getKey() + ": " + String.join(", ", entry.getValue()));
        }
    }
}



/*
run:

bra : [branches]
the : [the, the, the, the]
sur : [surprised]
see : [see]
inh : [inhabitants]
wer : [were]
low : [lowly, lowland, lower]
tre : [trees]

bra: branches
the: the, the, the, the
sur: surprised
see: see
inh: inhabitants
wer: were
low: lowly, lowland, lower
tre: trees

*/

 



answered Mar 13 by avibootz
...