Bird
0
0
DSA Cprogramming~5 mins

KMP Pattern Matching Algorithm in DSA C - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does KMP stand for in the KMP Pattern Matching Algorithm?
KMP stands for Knuth-Morris-Pratt, the last names of the three people who created this efficient pattern searching algorithm.
Click to reveal answer
beginner
What is the main advantage of the KMP algorithm over the naive pattern matching approach?
KMP avoids rechecking characters that have already been matched by using a precomputed table, making it faster especially for large texts and patterns.
Click to reveal answer
intermediate
What is the purpose of the 'lps' (Longest Prefix Suffix) array in KMP?
The 'lps' array stores the length of the longest proper prefix of the pattern that is also a suffix for each prefix of the pattern. It helps decide where to continue matching after a mismatch.
Click to reveal answer
intermediate
In KMP, what happens when a mismatch occurs after some matches?
Instead of starting over, KMP uses the 'lps' array to skip characters and continue matching from the longest prefix that matches a suffix, saving time.
Click to reveal answer
intermediate
What is the time complexity of the KMP Pattern Matching Algorithm?
The time complexity is O(n + m), where n is the length of the text and m is the length of the pattern, because each character is processed at most twice.
Click to reveal answer
What does the 'lps' array in KMP represent?
ALongest prefix which is also suffix for pattern prefixes
BLength of the pattern
CPositions of pattern matches in text
DNumber of mismatches found
What is the main benefit of using KMP over naive pattern matching?
AIt uses less memory
BIt finds all substrings in constant time
CIt works only on small texts
DIt avoids rechecking characters after mismatch
If a mismatch occurs at pattern index 3, where does KMP jump next?
ABack to pattern start
BTo the index given by lps[2]
CTo the next character in text
DTo the end of the pattern
What is the worst-case time complexity of KMP?
AO(n*m)
BO(n^2)
CO(n + m)
DO(m^2)
Which step is NOT part of the KMP algorithm?
ARestart matching from the beginning after mismatch
BPrecompute the lps array
CCompare characters of text and pattern
DUse lps to skip unnecessary comparisons
Explain how the 'lps' array is constructed and how it helps in pattern matching.
Think about prefixes and suffixes in the pattern.
You got /3 concepts.
    Describe the steps KMP takes when a mismatch happens during pattern matching.
    Focus on how KMP avoids rechecking characters.
    You got /3 concepts.