Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/main/java/com/thealgorithms/strings/NaivePatternSearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.thealgorithms.strings;

import java.util.ArrayList;
import java.util.List;

public final class NaivePatternSearch {

// Private constructor to prevent instantiation
private NaivePatternSearch() {
throw new UnsupportedOperationException("Utility class");
}

/**
* Naive pattern searching algorithm.
*
* @param text the text to be searched
* @param pattern the pattern to search for
* @return list of starting indices where pattern is found
* @throws IllegalArgumentException if text or pattern is null
*/
public static List<Integer> search(String text, String pattern) {
if (text == null || pattern == null) {
throw new IllegalArgumentException("Text and pattern must not be null");
}

List<Integer> result = new ArrayList<>();
int n = text.length();
int m = pattern.length();

for (int i = 0; i <= n - m; i++) {
int j;
for (j = 0; j < m; j++) {
if (text.charAt(i + j) != pattern.charAt(j)) {
break;
}
}
if (j == m) {
result.add(i);
}
}

return result;
}
}