From bbb645c2d01dc5ace5b5a67567db8bd74a0f5c56 Mon Sep 17 00:00:00 2001 From: Karthikn-VR Date: Tue, 26 Aug 2025 21:51:36 +0530 Subject: [PATCH 1/2] add : NaivePatternSearch.java --- .../strings/NaivePatternSearch.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/main/java/com/thealgorithms/strings/NaivePatternSearch.java diff --git a/src/main/java/com/thealgorithms/strings/NaivePatternSearch.java b/src/main/java/com/thealgorithms/strings/NaivePatternSearch.java new file mode 100644 index 000000000000..0708007537e6 --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/NaivePatternSearch.java @@ -0,0 +1,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 search(String text, String pattern) { + List 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; + } +} From 8685c8635a0f9924beb59f671b8017fdc28f00e0 Mon Sep 17 00:00:00 2001 From: Karthikn-VR Date: Tue, 26 Aug 2025 22:02:30 +0530 Subject: [PATCH 2/2] Updated : NaivePatternSearch.java --- .../strings/NaivePatternSearch.java | 37 ++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/thealgorithms/strings/NaivePatternSearch.java b/src/main/java/com/thealgorithms/strings/NaivePatternSearch.java index 0708007537e6..1b284b1a1ba8 100644 --- a/src/main/java/com/thealgorithms/strings/NaivePatternSearch.java +++ b/src/main/java/com/thealgorithms/strings/NaivePatternSearch.java @@ -1,33 +1,44 @@ -package strings; +package com.thealgorithms.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 { +public final class NaivePatternSearch { + + // Private constructor to prevent instantiation + private NaivePatternSearch() { + throw new UnsupportedOperationException("Utility class"); + } /** - * Finds all occurrences of a pattern in a given text using - * the naive substring search algorithm. + * Naive pattern searching 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. + * @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 search(String text, String pattern) { + if (text == null || pattern == null) { + throw new IllegalArgumentException("Text and pattern must not be null"); + } + List 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)) { + 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; } }