String text = "ABABDABACDABABCABAB"; String pattern = "ABABCABAB"; List<Integer> matches = search(text, pattern); if (!matches.isEmpty()) { System.out.println("Pattern found at positions: " + matches); } else { System.out.println("Pattern not found in the text."); } } } Output