📖 Scenario: Imagine you are building a simple text search tool that quickly finds if a small word (pattern) appears inside a larger text. This is like searching for a friend's name in a long list of names.
🎯 Goal: You will build the KMP (Knuth-Morris-Pratt) pattern matching algorithm step-by-step in Go. This algorithm helps find the pattern efficiently by avoiding repeated checks.
📋 What You'll Learn
Create a function to build the longest prefix suffix (LPS) array for the pattern
Create a function to use the LPS array to search the pattern in the text
Use clear variable names: pattern, text, lps, i, j
Print the starting index of the pattern in the text if found, or -1 if not found
💡 Why This Matters
🌍 Real World
Text search is used in search engines, word processors, and DNA sequence analysis to quickly find patterns inside large data.
💼 Career
Understanding KMP helps in roles involving algorithms, software development, and data processing where efficient string matching is important.
Progress0 / 4 steps
1
Create the pattern and text variables
Create two string variables called pattern with value "ABABCABAB" and text with value "ABABDABACDABABCABAB".
DSA Go
Hint
Use := to create and assign the strings pattern and text.
2
Build the LPS array for the pattern
Create a function called computeLPSArray that takes a string pattern and returns an int slice called lps. Initialize lps with zeros and use variables length and i to fill it according to the KMP prefix logic.
DSA Go
Hint
Follow the KMP algorithm to fill the lps array. Use a for loop starting from i = 1 and compare characters at i and length.
3
Implement the KMP search function
Create a function called KMPsearch that takes text and pattern strings and returns the starting index of the first match or -1 if not found. Use variables i and j to track positions in text and pattern. Use the computeLPSArray function to get the lps array and apply the KMP matching logic.
DSA Go
Hint
Use two pointers i and j to scan text and pattern. Use the lps array to skip characters when mismatch happens.
4
Print the index of the pattern in the text
Call the KMPsearch function with text and pattern and print the returned index.
DSA Go
Hint
Call KMPsearch(text, pattern) and print the result using fmt.Println.
Practice
(1/5)
1. What is the main purpose of the KMP (Knuth-Morris-Pratt) algorithm in pattern matching?
easy
A. To efficiently find occurrences of a pattern in a text without rechecking characters
B. To sort the characters of the pattern alphabetically
C. To reverse the text before searching
D. To count the number of vowels in the text
Solution
Step 1: Understand KMP's goal
KMP is designed to find pattern matches in text efficiently by avoiding repeated comparisons.
Step 2: Compare options with KMP's function
Only To efficiently find occurrences of a pattern in a text without rechecking characters describes this efficient search without rechecking characters.
Final Answer:
To efficiently find occurrences of a pattern in a text without rechecking characters -> Option A
Quick Check:
KMP purpose = efficient pattern search [OK]
Hint: KMP avoids rechecking characters during pattern search [OK]
Common Mistakes:
Thinking KMP sorts or modifies the text
Confusing KMP with simple substring search
Assuming KMP counts characters instead of searching
2. Which of the following is the correct way to declare the prefix table (lps array) in Go for KMP algorithm?
easy
A. lps := []string{len(pattern)}
B. lps := make([]int, len(pattern))
C. lps := make([]bool, len(pattern))
D. lps := new(int)
Solution
Step 1: Identify correct Go syntax for integer slice
In Go, make([]int, size) creates an integer slice of given size.
Step 2: Check options for correct slice creation
lps := make([]int, len(pattern)) correctly creates an int slice for lps. Others use wrong types or syntax.
Final Answer:
lps := make([]int, len(pattern)) -> Option B
Quick Check:
Go slice for lps = make([]int, len(pattern)) [OK]
Hint: Use make([]int, size) to create integer slices in Go [OK]
Common Mistakes:
Using wrong slice type like []string or []bool
Using new(int) which creates a pointer, not slice
Incorrect slice initialization syntax
3. What will be the output of the following Go code snippet that uses KMP to find pattern occurrences?