Quick actions

cmd+k|ctrl+k

Navigation

Languages

Caveman Number Count

Snippet info

Language

Java

Visibility

public

Author

seaker

Created

2021-05-05T12:41:55.782777Z

Updated

2021-05-05T12:42:35.395386Z

import java.util.*;

class Caveman {
    static String[] wordNumberList = { "ook", "ookook", "oog", "ooga", "ug", "mook", "mookmook", "oogam", "oogum", "ugug" };
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while (scan.hasNext()) {
            String s = scan.nextLine();
            System.out.println(String.format("%s => %d", s, countNeanderthalWords(s)));
        }
        scan.close();
    }

    public static int countNeanderthalWords(String line) {
        if (line.length() == 0) return 1;

        int counter = 0;
        for (String wordNumber : wordNumberList) {
            if (line.startsWith(wordNumber)) {
                counter += countNeanderthalWords(line.substring(wordNumber.length()));
            }
        }
        return counter;
    }
}
INFO