On se retrouve aujourd'hui pour la solution du précédent #KataOfTheWeek proposé par Jonathan en début de semaine !
La solution que je propose est basée sur les streams Java :
class Repetition {
public static final int PATTERN_SIZE_MIN = 2;
public static final int REPETITION_MIN = 2;
public static void main(String[] args) {
String s = "12121";
IntStream.range(PATTERN_SIZE_MIN, s.length()).boxed()
.flatMap(patternSize ->
IntStream.rangeClosed(0, s.length() - patternSize).mapToObj(i -> Map.entry(i, i + patternSize))
)
.collect(Collectors.groupingBy(e -> s.substring(e.getKey(), e.getValue()), Collectors.counting()))
.entrySet().stream().filter(e -> e.getValue() >= REPETITION_MIN)
.forEach(System.out::println);
}
}
A bientôt pour un nouveau #KataOfTheWeek !