Optimized Algorithm for Pattern Searching
Question: We have discussed the Naive String matching algorithm here. Consider a situation where all characters of a pattern are different. Can we modify the original Naive String Matching algorithm so that it works better for these types of patterns? If we can, then what are the changes to the original algorithm?
Solution: In the original Naive String matching algorithm, we always slide the pattern by 1. When all characters of the pattern are different, we can slide the pattern by more than 1. Let us see how can we do this. When a mismatch occurs after j matches, we know that the first character of the pattern will not match the j-matched characters because all characters of the pattern are different. So we can always slide the pattern by j without missing any valid shifts. Following is the modified code that is optimized for the special patterns.
N.B: This algorithm only works when the search pattern pat has all different characters.
C++
#include <bits/stdc++.h>
using namespace std;
void search(string pat, string txt)
{
int M = pat.size();
int N = txt.size();
int i = 0;
while (i <= N - M) {
int j;
for (j = 0; j < M; j++)
if (txt[i + j] != pat[j])
break ;
if (j == M)
{
cout << "Pattern found at index " << i << endl;
i = i + M;
}
else if (j == 0)
i = i + 1;
else
i = i + j;
}
}
int main()
{
string txt = "ABCEABCDABCEABCD" ;
string pat = "ABCD" ;
search(pat, txt);
return 0;
}
|
C
#include <stdio.h>
#include <string.h>
void search( char pat[], char txt[])
{
int M = strlen (pat);
int N = strlen (txt);
int i = 0;
while (i <= N - M) {
int j;
for (j = 0; j < M; j++)
if (txt[i + j] != pat[j])
break ;
if (j == M)
{
printf ( "Pattern found at index %d \n" , i);
i = i + M;
}
else if (j == 0)
i = i + 1;
else
i = i + j;
}
}
int main()
{
char txt[] = "ABCEABCDABCEABCD" ;
char pat[] = "ABCD" ;
search(pat, txt);
return 0;
}
|
Java
class GFG {
static void search(String pat, String txt)
{
int M = pat.length();
int N = txt.length();
int i = 0 ;
while (i <= N - M) {
int j;
for (j = 0 ; j < M; j++)
if (txt.charAt(i + j) != pat.charAt(j))
break ;
if (j == M)
{
System.out.println( "Pattern found at index " + i);
i = i + M;
}
else if (j == 0 )
i = i + 1 ;
else
i = i + j;
}
}
public static void main(String[] args)
{
String txt = "ABCEABCDABCEABCD" ;
String pat = "ABCD" ;
search(pat, txt);
}
}
|
Python3
def search(pat, txt):
M = len (pat)
N = len (txt)
i = 0
while i < = N - M:
for j in range (M):
if txt[i + j] ! = pat[j]:
break
j + = 1
if j = = M:
print ( "Pattern found at index " + str (i))
i = i + M
elif j = = 0 :
i = i + 1
else :
i = i + j
txt = "ABCEABCDABCEABCD"
pat = "ABCD"
search(pat, txt)
|
C#
using System;
class GFG {
static void search( string pat, string txt)
{
int M = pat.Length;
int N = txt.Length;
int i = 0;
while (i <= N - M) {
int j;
for (j = 0; j < M; j++)
if (txt[i + j] != pat[j])
break ;
if (j == M)
{
Console.WriteLine( "Pattern found at index " + i);
i = i + M;
}
else if (j == 0)
i = i + 1;
else
i = i + j;
}
}
static void Main()
{
string txt = "ABCEABCDABCEABCD" ;
string pat = "ABCD" ;
search(pat, txt);
}
}
|
PHP
<?php
function search( $pat , $txt )
{
$M = strlen ( $pat );
$N = strlen ( $txt );
$i = 0;
while ( $i <= $N - $M )
{
$j ;
for ( $j = 0; $j < $M ; $j ++)
if ( $txt [ $i + $j ] != $pat [ $j ])
break ;
if ( $j == $M )
{
echo ( "Pattern found at index $i" . "\n" );
$i = $i + $M ;
}
else if ( $j == 0)
$i = $i + 1;
else
$i = $i + $j ;
}
}
$txt = "ABCEABCDABCEABCD" ;
$pat = "ABCD" ;
search( $pat , $txt );
?>
|
Javascript
<script>
function search(pat, txt)
{
let M = pat.length;
let N = txt.length;
let i = 0;
while (i <= N - M)
{
let j;
for (j = 0; j < M; j++)
if (txt[i + j] != pat[j])
break ;
if (j == M)
{
document.write( "Pattern found at index " +
i + "<br/>" );
i = i + M;
}
else if (j == 0)
i = i + 1;
else
i = i + j;
}
}
let txt = "ABCEABCDABCEABCD" ;
let pat = "ABCD" ;
search(pat, txt);
</script>
|
Output
Pattern found at index 4
Pattern found at index 12
Time Complexity: O(N*M), where N is the length of txt and M is the pat length.
Auxiliary Space: O(1), We are not using any extra space.
Last Updated :
14 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...