forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNaivePatternSearch.java
More file actions
33 lines (29 loc) · 952 Bytes
/
NaivePatternSearch.java
File metadata and controls
33 lines (29 loc) · 952 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package strings;
import java.util.ArrayList;
import java.util.List;
/**
* Naive Pattern Searching algorithm.
* Reference: https://en.wikipedia.org/wiki/String-searching_algorithm#Na%C3%AFve_string_search
*/
public class NaivePatternSearch {
/**
* Finds all occurrences of a pattern in a given text using
* the naive substring search algorithm.
*
* @param text The text in which to search.
* @param pattern The pattern to search for.
* @return List of starting indices where the pattern is found.
*/
public static List<Integer> search(String text, String pattern) {
List<Integer> result = new ArrayList<>();
int n = text.length();
int m = pattern.length();
for (int i = 0; i <= n - m; i++) {
String sub = text.substring(i, i + m);
if (sub.equals(pattern)) {
result.add(i);
}
}
return result;
}
}