How do I get all the indices(including overlapping) of the string where a pattern is matching. I have this poc code.
public static void main(){
String input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
Pattern pattern = Pattern.compile("aaa");
Matcher matcher = pattern.matcher(input);
List<Integer> all = new ArrayList<>();
while (matcher.find()) {
all.add(matcher.start());
}
System.out.println(all);
}
Output:
[0, 3, 6, 9, 12, 15, 18, 21, 24, 27]
It does not consider overlapping patterns. All the matching indices should be:
[0, 1, 2, 3, 4, .....27]
I know it is easily doable by KMP, but
Can we do it using Pattern and Matcher?