Skip to content

Commit 199b96b

Browse files
author
Mrinal Chauhan
committed
fix: updated code to fix run_infer errors
1 parent 704035d commit 199b96b

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/main/java/com/thealgorithms/strings/Zalgorithm.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ private Zalgorithm() {
1111
}
1212

1313
/**
14-
* Finds the occurrences of a pattern in a text using Z-algorithm.
14+
* Finds occurrences of a pattern in a text using Z-algorithm.
1515
*
1616
* @param text the input text in which we are searching the pattern
1717
* @param pattern the pattern to search for
@@ -22,11 +22,17 @@ public static List<Integer> findPatternOccurrences(String text, String pattern)
2222
int[] zArray = calculateZ(combined);
2323
List<Integer> occurrences = new ArrayList<>();
2424
int patternLength = pattern.length();
25-
// performing z algorithm
25+
2626
for (int i = patternLength + 1; i < zArray.length; i++) {
2727
if (zArray[i] == patternLength) {
2828
occurrences.add(i - patternLength - 1);
29-
i += patternLength - 1;
29+
30+
// Modification to handle single and multi-character patterns differently:
31+
// Skip to next position for non-overlapping matches only if pattern length > 1
32+
if (patternLength > 1) {
33+
i += patternLength - 1; // Skip positions for non-overlapping matches
34+
}
35+
// For single-character patterns, continue without skipping to capture overlaps
3036
}
3137
}
3238
return occurrences;

0 commit comments

Comments
 (0)