Java Program to Implement the String Search Algorithm for Short Text Sizes
Last Updated :
05 Feb, 2021
Pattern searching is a crucial problem in computer science. When we do search for a string in notepad/word file or browser or database, pattern searching algorithms are used to show the search results.
A typical problem statement would be-
Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function search(char pat[], char txt[]) that prints all occurrences of pat[] in txt[].
Examples:
Input: txt[] = "THIS IS A TEST TEXT"
pat[] = "TExT"
Output: Pattern found at index 15
Input: txt[] = "AABAACAADAABAABA"
pat[] = "AABA"
Output: Pattern found at index 0
Pattern found at index 9
Pattern found at index 12
In this program, a text and a pattern are given as an input and a pattern is searched in the text, and we get all the instances of the pattern as an output.
Algorithm:
- Take text and pattern as input.
- Run an outer for loop from 0 to length of text-length of the pattern.
- Run an inner loop from 0 to the length of the pattern.
- Through this,, for each character at each index in the text starting from that index till index+length of pattern, the pattern is searched in the text.
- If the pattern is found, print the index of the outer loop at which that pattern is found in the text.
- Else if the pattern is not found, then print it is not found.
Below is the implementation of the above approach:
Java
// Java Program to Implement the String Search
// Algorithm for Short Text Sizes
import java.io.*;
class GFG {
public static void main(String[] args)
{
String text = "geeksforgeeks is a coding website for geeks";
String pattern = "geeks";
// calling the method that is designed for
// printing the instances of pattern
// found in the text string
stringMatch(text, pattern);
}
public static void stringMatch(String text, String pattern)
{
int len_t = text.length();
int len_p = pattern.length();
int k = 0, i = 0, j = 0;
// loop to find out the position Of searched pattern
for (i = 0; i <= (len_t - len_p); i++) {
for (j = 0; j < len_p; j++)
{
if (text.charAt(i + j) != pattern.charAt(j))
break;
}
if (j == len_p)
{
k++;
System.out.println("Pattern Found at Position: " + i);
}
}
if (k == 0)
System.out.println("No Match Found!");
else
System.out.println("Total Instances Found = " + k);
}
}
OutputPattern Found at Position: 0
Pattern Found at Position: 8
Pattern Found at Position: 38
Total Instances Found = 3
Worst-case Time Complexity: O(m(n-m+1))
Effective approach:
KMP algorithm is an effective way to search for a pattern inside text. While traversal when a mismatch is detected, some characters in the text of the next window are already known. Taking this advantage time complexity gets reduce to O(n).
Below is the implementation of the effective approach:
Java
// Java Program to Implement the String Search
// Algorithm for Short Text Sizes
class KMP_String_Matching {
void KMPSearch(String pat, String txt)
{
int M = pat.length();
int N = txt.length();
// create lps[] that will hold the longest
// prefix suffix values for pattern
int lps[] = new int[M];
int j = 0; // index for pat[]
// Preprocess the pattern (calculate lps[]
// array)
computeLPSArray(pat, M, lps);
int i = 0; // index for txt[]
while (i < N) {
if (pat.charAt(j) == txt.charAt(i)) {
j++;
i++;
}
if (j == M) {
System.out.println("Found pattern "
+ "at index " + (i - j));
j = lps[j - 1];
}
// mismatch after j matches
else if (i < N
&& pat.charAt(j) != txt.charAt(i)) {
// Do not match lps[0..lps[j-1]] characters,
// they will match anyway
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
}
void computeLPSArray(String pat, int M, int lps[])
{
// length of the previous longest prefix suffix
int len = 0;
int i = 1;
lps[0] = 0; // lps[0] is always 0
// the loop calculates lps[i] for i = 1 to M-1
while (i < M) {
if (pat.charAt(i) == pat.charAt(len)) {
len++;
lps[i] = len;
i++;
}
else // (pat[i] != pat[len])
{
// This is tricky. Consider the example.
// AAACAAAA and i = 7. The idea is similar
// to search step.
if (len != 0) {
len = lps[len - 1];
// Also, note that we do not increment
// i here
}
else // if (len == 0)
{
lps[i] = len;
i++;
}
}
}
}
// Driver program to test above function
public static void main(String args[])
{
String text
= "geeksforgeeks is a coding website for geeks";
String pattern = "geeks";
KMP_String_Matching effective
= new KMP_String_Matching();
effective.KMPSearch(pattern, text);
}
}
OutputFound pattern at index 0
Found pattern at index 8
Found pattern at index 38
Time Complexity: O(n)
Similar Reads
Java Program to Implement Bitap Algorithm for String Matching The Bitap Algorithm is an approximate string matching algorithm. The algorithm tells whether a given text contains a substring which is "approximately equal" to a given pattern. Here approximately equal states that if the substring and pattern are within a given distance k of each other. The algorit
5 min read
Java Program to Implement ZhuâTakaoka String Matching Algorithm Zhu-Takaoka String Matching Algorithm is a Variant of Boyer Moore Algorithm for Pattern Matching in a String. There is a slight change in the concept of Bad Maps in this algorithm. The concept of Good Suffixes remains as same as that of Boyer Moore's but instead of using a single character for Bad S
7 min read
Java Program to Implement Wagner and Fisher Algorithm for Online String Matching The Wagner-Fischer Algorithm is a dynamic programming algorithm that measures the Levenshtein distance or the edit distance between two strings of characters. Levenshtein Distance(LD) calculates how similar are the two strings. The distance is calculated by three parameters to transform the string1
3 min read
Java Program to Search an Element in Vector A vector in Java is a dynamic array that can be resized as needed. It is synchronized, which means it is safe in multi-threaded programs. To find an element in a Vector we have to loop through its elements to find a match. In this article, we will learn how to search for a component in a Vector usin
3 min read
Implementing Rabin Karp Algorithm Using Rolling Hash in Java There are so many pattern searching algorithms for the string. KMP algorithm, Z algorithm Rabin Karp algorithm, etc these algorithms are the optimization of Naive Pattern searching Algorithm. Naive Pattern Searching Algorithm: Input : "AABACACAACAC" Pattern : "CAC" Output : [4,9] AABACACAACAC Implem
5 min read
Java Program to Search a Particular Word in a String Using Regex In Java string manipulation, searching for specific words is a fundamental task. Regular expressions (regex) offer a powerful and flexible approach to achieve this search. It matches the patterns simply by comparing substrings. In this article, we will learn how to search for a particular word in a
3 min read
Java Program To Check If A String Is Substring Of Another Write a java program for a given two strings s1 and s2, find if s1 is a substring of s2. If yes, return the index of the first occurrence, else return -1. Examples : Input: s1 = "for", s2 = "geeksforgeeks"Output: 5Explanation: String "for" is present as a substring of s2. Input: s1 = "practice", s2
3 min read
Java Program to find the Last Index of a Particular Word in a String To find out the last occurrence of a character or string from a given string across which the substring or character is been searched over and returning the index value of the character or substring is found. Real-life Example: Consider an example of a book. The book contains pages where pages conta
4 min read
Sort and Search an Element in Java In Java sorting and searching an element in an array is very easy. Unlike C, where we have to make all the functions to work, Java has inbuilt functions to do the same work. To sort an array there is a sort function and to search an element in a sorted array there is a binarySearch() function. To le
3 min read
Java Regex Programs - Basic to Advanced In Java, Regular Expressions or Regex (in short) in Java is an API for defining String patterns that can be used for searching, manipulating, and editing a string in Java. Regular Expressions in Java are provided under java.util.regex package. This consists of 3 classes and 1 interface. In Java Inte
5 min read