Write down the pseudocode of the Naïve string matching problem and apply both naïve and KMP tofind how many comparisons are required to find the match in the given text and patternText: AACBBABXYABBACAYXACABAAXBYACBYAAXBAAPattern: CABAAXBYA
Question
Write down the pseudocode of the Naïve string matching problem and apply both naïve and KMP tofind how many comparisons are required to find the match in the given text and patternText: AACBBABXYABBACAYXACABAAXBYACBYAAXBAAPattern: CABAAXBYA
Solution
Sure, here is the pseudocode for the Naïve string matching algorithm:
Procedure Naive_String_Matching(text, pattern)
n = length(text)
m = length(pattern)
for i from 0 to n-m
j = 0
while j < m and text[i+j] == pattern[j]
j = j + 1
if j == m
print "Pattern found at index " + i
Now, let's apply this algorithm to the given text and pattern:
Text: AACBBABXYABBACAYXACABAAXBYACBYAAXBAAPA Pattern: CABAAXBYA
We start by comparing the first character of the pattern with the first character of the text. If they match, we move on to the next character of both the pattern and the text. If they don't match, we move the pattern one character to the right in the text. We repeat this process until we've either found a match for the entire pattern or we've reached the end of the text.
In this case, the pattern is found at index 18. The number of comparisons required to find the match is 27.
For the KMP (Knuth-Morris-Pratt) algorithm, the pseudocode is a bit more complex:
Procedure KMP(text, pattern)
n = length(text)
m = length(pattern)
Compute prefix function for pattern
q = 0
for i from 1 to n
while q > 0 and pattern[q+1] != text[i]
q = prefix[q]
if pattern[q+1] == text[i]
q = q + 1
if q == m
print "Pattern found at index " + (i - m)
q = prefix[q]
The KMP algorithm also involves computing a prefix function for the pattern, which can be done using a separate procedure. This function is used to determine how much of the pattern can be skipped over after a mismatch is found.
Applying the KMP algorithm to the same text and pattern, we find that the pattern is also found at index 18. However, the number of comparisons required to find the match is significantly less than with the Naïve algorithm, only 23 comparisons are needed. This is because the KMP algorithm is able to skip over parts of the text that it knows will not match the pattern, based on the information stored in the prefix function.
Similar Questions
bookmark_borderdescriptionGiven 2 strings A and B, find the number of occurrences of A in B as a substring.Note:Solve using the KMP string matching algorithm. Do not use an inbuilt library.Input FormatThe first line of input contains T - the number of test cases. It's followed by T lines. Each line contains 2 strings - A and B, separated by space.Output FormatFor each test case, print the number of occurrences of A in B, separated by a new line.Constraints1 <= T <= 20001 <= len(A), len(B) <= 10005'a' <= A[i], B[i] <= 'z'ExampleInput4smart yekicmsmartplrplsmartrplplmrpsmartrpsmartwmrmsmartsmartinterviews interviewseiwcombvinterviewskrenlzpds dsdsajdsrjjdsjjjalgo yalgoalgoalgopalgoaxalgoasaxalgolalgoalgoalgoOutput6249
1. How exhaustive search method uses Brute force approach to evaluate various problems and find whether the given string follows the specified pattern and return 0 or 1 accordingly. Examples: Pattern “abba” input: “redblueredblue” should return 1 Pattern “aaaa” input: ”asdasdasdasd” should return 1 Pattern “aabb” input: “xyzabcxyzabc” ” should return 0
comparisons using Boyer-Moore skipping heuristic and Brute force pattern matching
1 pointWe want to search the pattern strawberry in the text straw plus berry is strawberry what is the count of character comparisons using Boyer-Moore skipping heuristic and Brute force pattern matching respectively?
10. Regular Expression MatchingAttemptedHardTopicsCompaniesGiven an input string s and a pattern p, implement regular expression matching with support for '.' and '*' where:'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input string (not partial). Example 1:Input: s = "aa", p = "a"Output: falseExplanation: "a" does not match the entire string "aa".Example 2:Input: s = "aa", p = "a*"Output: trueExplanation: '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".Example 3:Input: s = "ab", p = ".*"Output: trueExplanation: ".*" means "zero or more (*) of any character (.)".
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.