Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Exam (elaborations)

Dsa Module 5- String Matching Algorithms & Naive Method Analysis

Rating
-
Sold
-
Pages
17
Grade
A+
Uploaded on
25-02-2026
Written in
2025/2026

Dsa Module 5- String Matching Algorithms & Naive Method Analysis

Institution
Course

Content preview

lOMoARcPSD|44613495




Dsa Module - 5 notes


Data Structure and Algorithms for Problem solving (Visvesvaraya Technological
University)




Scan to open on Studocu




Studocu is not sponsored or endorsed by any college or university
Downloaded by Samuel Wachira ndiang'ui ()

, lOMoARcPSD|44613495




Module – 5
String - Matching Algorithms




1. The Naive String-Matching Algorithm

The naive algorithm finds all valid shifts using a loop that checks the condition
P (1.. m) = T (s + 1… s + m) for each of the n - m + 1 possible values of s.



NAIVE-STRING-MATCHER (T, P)

1 n = T.length

2 m = P.length

3 for s = 0 to n - m

4 if P [1..m] == T [s + 1 … s + m]

5 print “Pattern occurs with shift” s


Show the comparisons the naive string matcher makes for the pattern P = 0001 in the text
T = 000010001010001.

Text (T): 000010001010001

Pattern (P): 0001 (length m = 4)

Text length: n = 15

Number of shifts: n - m + 1 = 12



We try shifts from s = 0 to 11, comparing substrings T[s:s+4] with P.




Downloaded by Samuel Wachira ndiang'ui ()

, lOMoARcPSD|44613495




Shift s T substring T[s:s+4] Compare with P Match? Comparisons

0 0000 0001 ✗ 4 (mismatch at 4th)

1 0000 0001 ✗ 4

2 0001 0001 ✓ 4

3 0010 0001 ✗ 1

4 0100 0001 ✗ 1

5 1000 0001 ✗ 1

6 0001 0001 ✓ 4

7 0010 0001 ✗ 1

8 0101 0001 ✗ 1

9 1010 0001 ✗ 1

10 0100 0001 ✗ 1

11 1001 0001 ✗ 1

Matches found at shifts: s = 2 and s = 6
Total comparisons: 4+4+4+1+1+1+4+1+1+1+1+1 = 24



Suppose that all characters in the pattern P are different. Show how to accelerate NAIVE-
STRING-MATCHER to run in time O(n).

If all characters in P are distinct:

• When a mismatch is found at position j, we can safely shift P by j positions instead of
just 1.

• Since no prefix of P is a suffix, there's no point in checking intermediate shifts.

This results in skipping comparisons and advances faster, which leads to O(n) time in the best
and average cases.

You can modify the loop:

s=0
while s <= n - m:




Downloaded by Samuel Wachira ndiang'ui ()

Written for

Institution
Study
Course

Document information

Uploaded on
February 25, 2026
Number of pages
17
Written in
2025/2026
Type
Exam (elaborations)
Contains
Questions & answers

Subjects

$23.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
Writewise Chamberlain College Of Nursing
Follow You need to be logged in order to follow users or courses
Sold
49
Member since
1 year
Number of followers
5
Documents
1828
Last sold
3 weeks ago
Writewise - Stuvia US

Writewise - Stuvia US Certified tutor, offering accurate, reliable, and current study materials to support students in their exam preparation and assignments. Aiming to provide the best resources, such as summaries, nursing exam test. Up-to-date exams and assignments, Detailed test banks with verified questions and answers, Elaborate exam solutions, Case studies and discussions Customized package deals tailored to your needs. I’m committed to providing only high-quality documents to ensure the best outcomes. Get instant access to expertly prepared materials designed to help you excel in your academic journey. Reach out today and take a step closer to achieving your goals! Always be Encouraged to leave a review after sale, all complements and comments, positive &amp; Negative are appreciated to guide for better changes.

Read more Read less
3.0

4 reviews

5
1
4
0
3
2
2
0
1
1

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions