0% found this document useful (0 votes)
7 views2 pages

Naive string matching algorithm

The Naive String Matching Algorithm is a simple technique for finding occurrences of a pattern within a text by comparing the pattern to every possible substring of the text. It involves checking each substring of the text against the pattern character by character. An example illustrates the matching process with a specific text and pattern, showing the indices where matches occur.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Naive string matching algorithm

The Naive String Matching Algorithm is a simple technique for finding occurrences of a pattern within a text by comparing the pattern to every possible substring of the text. It involves checking each substring of the text against the pattern character by character. An example illustrates the matching process with a specific text and pattern, showing the indices where matches occur.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Naive string matching algorithm

The Naive String Matching Algorithm is a straightforward method used to find


occurrences of a pattern (substring) within a text (larger string). It checks for a
match by comparing the pattern to every possible substring of the text, character by
character.

How It Works
Let:
 T be the text of length n
 P be the pattern of length m

Example
Text (T): "AABAACAADAABAABA" (length=16)
Pattern (P): "AABA" (length=4)

Step-by-Step Matching
Let's find all positions where "AABA" appears in the text:
1. Check T[0:4] = "AABA" → ✅ Match at index 0
2. T[1:5] = "ABAA" → ❌ No match
3. T[2:6] = "BAAC" → ❌ No match
4. T[3:7] = "AACA" → ❌ No match
5. T[4:8] = "ACAA" → ❌ No match
6. T[5:9] = "CAAD" → ❌ No match
7. T[6:10] = "AADA" → ❌ No match
8. T[7:11] = "ADAA" → ❌ No match
9. T[8:12] = "DAAB" → ❌ No match
10. T[9:13] = "AABA" → ✅ Match at index 9
11. T[10:14] = "ABAA" → ❌ No match
12. T[11:15] = "BAAB" → ❌ No match
13. T[12:16] = "AABA" → ✅ Match at index 12
Steps:
1. Start from the beginning of the text.
2. For each position i from 0 to n - m:
o Compare the substring T [i…i+m-1] with P.
o If all characters match, record the position i as a match.
3. Repeat until the end of the text is reached.

n= length of text
m=length of pattern

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy