DSA Explorer
QuicksortMerge SortBubble SortInsertion SortSelection SortHeap SortCounting SortRadix SortBucket SortShell SortTim SortCocktail Shaker SortComb SortGnome SortPancake SortPatience SortCycle SortStrand SortWiggle Sort (Wave Sort)Bead Sort (Gravity Sort)Binary Insertion SortBitonic SortBogo Sort (Stupid Sort)Stooge SortOdd-Even Sort (Brick Sort)Pigeonhole SortIntro Sort (Introspective Sort)Tree Sort (BST Sort)Dutch National Flag (3-Way Partitioning)
Binary SearchLinear SearchJump SearchInterpolation SearchExponential SearchTernary SearchFibonacci SearchQuick Select (k-th Smallest)Median of Medians (Deterministic Select)Hill climbingSimulated AnnealingTabu SearchBinary Tree DFS SearchSentinel Linear SearchDouble Linear SearchTernary Search (Unimodal Function)Search in 2D Matrix
Binary Search Tree (BST)StackQueueHash Table (Hash Map)Heap (Priority Queue)Linked ListTrie (Prefix Tree)Binary TreeTrie (Prefix Tree)Floyd's Cycle Detection (Tortoise and Hare)Merge Two Sorted Linked ListsCheck if Linked List is PalindromeFind Middle of Linked ListBalanced Parentheses (Valid Parentheses)Next Greater ElementInfix to Postfix ConversionMin Stack (O(1) getMin)Largest Rectangle in HistogramDaily Temperatures (Monotonic Stack)Evaluate Reverse Polish NotationInfix Expression Evaluation (Two Stacks)Min Heap & Max HeapSliding Window MaximumTrapping Rain WaterRotate Matrix 90 DegreesSpiral Matrix TraversalSet Matrix ZeroesHash Table with ChainingOpen Addressing (Linear Probing)Double HashingCuckoo Hashing
Depth-First Search (DFS)Breadth-First Search (BFS)Dijkstra's AlgorithmFloyd-Warshall AlgorithmKruskal's AlgorithmPrim's AlgorithmTopological SortA* Pathfinding AlgorithmKahn's Algorithm (Topological Sort)Ford-Fulkerson Max FlowEulerian Path/CircuitBipartite Graph CheckBorůvka's Algorithm (MST)Bidirectional DijkstraPageRank AlgorithmBellman-Ford AlgorithmTarjan's Strongly Connected ComponentsArticulation Points (Cut Vertices)Find Bridges (Cut Edges)Articulation Points (Cut Vertices)Finding Bridges (Cut Edges)
0/1 Knapsack ProblemLongest Common Subsequence (LCS)Edit Distance (Levenshtein Distance)Longest Increasing Subsequence (LIS)Coin Change ProblemFibonacci Sequence (DP)Matrix Chain MultiplicationRod Cutting ProblemPalindrome Partitioning (Min Cuts)Subset Sum ProblemWord Break ProblemLongest Palindromic SubsequenceMaximal Square in MatrixInterleaving StringEgg Drop ProblemUnique Paths in GridCoin Change II (Count Ways)Decode WaysWildcard Pattern MatchingRegular Expression MatchingDistinct SubsequencesMaximum Product SubarrayHouse RobberClimbing StairsPartition Equal Subset SumKadane's Algorithm (Maximum Subarray)
A* Search AlgorithmConvex Hull (Graham Scan)Line Segment IntersectionCaesar CipherVigenère CipherRSA EncryptionHuffman CompressionRun-Length Encoding (RLE)Lempel-Ziv-Welch (LZW)Canny Edge DetectionGaussian Blur FilterSobel Edge FilterHarris Corner DetectionHistogram EqualizationMedian FilterLaplacian FilterMorphological ErosionMorphological DilationImage Thresholding (Otsu's Method)Conway's Game of LifeLangton's AntRule 30 Cellular AutomatonFast Fourier Transform (FFT)Butterworth FilterSpectrogram (STFT)
Knuth-Morris-Pratt (KMP) AlgorithmRabin-Karp AlgorithmBoyer-Moore AlgorithmAho-Corasick AlgorithmManacher's AlgorithmSuffix ArraySuffix Tree (Ukkonen's Algorithm)Trie for String MatchingEdit Distance for StringsLCS for String MatchingHamming DistanceJaro-Winkler DistanceDamerau-Levenshtein DistanceBitap Algorithm (Shift-Or, Baeza-Yates-Gonnet)Rolling Hash (Rabin-Karp Hash)Manacher's AlgorithmZ AlgorithmLevenshtein Distance

Knuth-Morris-Pratt (KMP) Algorithm

String Algorithms
O(n + m) time, O(m) space
Advanced

An efficient string pattern matching algorithm that searches for occurrences of a 'word' within a 'text' by employing the observation that when a mismatch occurs, the word itself embodies sufficient information to determine where the next match could begin. Developed by Donald Knuth, Vaughan Pratt, and James H. Morris in 1977, it's one of the most important string algorithms with O(n+m) time complexity.

Prerequisites:
Strings
Arrays
Prefix concepts

Visualization

Interactive visualization for Knuth-Morris-Pratt (KMP) Algorithm

KMP String Matching Algorithm

Text:

A
B
A
B
D
A
B
A
C
D
A
B
A
B
C
A
B
A
B

Pattern:

A
B
A
B
C
A
B
A
B

• Time: O(n + m) where n = text length, m = pattern length

• Space: O(m) for LPS array

• KMP avoids re-scanning matched characters using LPS array

Interactive visualization with step-by-step execution

Implementation

Language:
1function kmpSearch(text: string, pattern: string): number[] {
2  const result: number[] = [];
3  const lps = buildLPS(pattern);
4  let i = 0; // text index
5  let j = 0; // pattern index
6  
7  while (i < text.length) {
8    if (text[i] === pattern[j]) {
9      i++;
10      j++;
11    }
12    
13    if (j === pattern.length) {
14      result.push(i - j);
15      j = lps[j - 1];
16    } else if (i < text.length && text[i] !== pattern[j]) {
17      if (j !== 0) {
18        j = lps[j - 1];
19      } else {
20        i++;
21      }
22    }
23  }
24  
25  return result;
26}
27
28function buildLPS(pattern: string): number[] {
29  const lps: number[] = Array(pattern.length).fill(0);
30  let len = 0;
31  let i = 1;
32  
33  while (i < pattern.length) {
34    if (pattern[i] === pattern[len]) {
35      len++;
36      lps[i] = len;
37      i++;
38    } else {
39      if (len !== 0) {
40        len = lps[len - 1];
41      } else {
42        lps[i] = 0;
43        i++;
44      }
45    }
46  }
47  
48  return lps;
49}

Deep Dive

Theoretical Foundation

KMP (Knuth-Morris-Pratt) builds a Longest Proper Prefix which is also Suffix (LPS) array during preprocessing. The LPS array stores, for each position i in the pattern, the length of the longest proper prefix that is also a suffix of pattern[0..i]. This crucial data structure allows the algorithm to avoid re-examining characters in the text when a mismatch occurs. Instead of backtracking in the text (like naive approach), KMP uses LPS to determine how far to shift the pattern. For example, if pattern='ABABC' and we match 'ABAB' then mismatch at 'C', we know 'AB' at the end matches 'AB' at the start, so we can continue matching from position 2 in pattern without moving back in text. This eliminates all backtracking, achieving O(n+m) time where n is text length and m is pattern length. The preprocessing itself takes O(m) time using a clever self-matching approach.

Complexity

Time

Best

O(n + m)

Average

O(n + m)

Worst

O(n + m)

Space

Required

O(m)

Applications

Industry Use

1

Text editors for Find/Replace functionality (VS Code, Sublime)

2

DNA sequence matching in bioinformatics

3

Plagiarism detection systems

4

Network intrusion detection (packet inspection)

5

Log file analysis and pattern monitoring

6

Substring search in databases (PostgreSQL, MySQL)

7

Compiler lexical analysis and tokenization

8

Data deduplication systems

9

Virus signature matching in antivirus software

Use Cases

Text search
DNA matching
Pattern recognition

Related Algorithms

Rabin-Karp Algorithm

A string-searching algorithm that uses hashing to find pattern(s) in a text. Developed by Michael O. Rabin and Richard M. Karp in 1987, it's particularly useful for multiple pattern search and plagiarism detection. Uses rolling hash for efficiency.

String Algorithms

Boyer-Moore Algorithm

One of the most efficient string searching algorithms in practice, using two heuristics: bad character rule and good suffix rule. Developed by Robert S. Boyer and J Strother Moore in 1977, it's the standard benchmark for practical string search, often outperforming other algorithms by skipping sections of text.

String Algorithms

Aho-Corasick Algorithm

A string-searching algorithm for locating elements of a finite set of strings (dictionary) within an input text. Invented by Alfred V. Aho and Margaret J. Corasick in 1975, it's a kind of dictionary-matching algorithm that simultaneously searches for all patterns in linear time, making it extremely efficient for multiple pattern matching.

String Algorithms

Manacher's Algorithm

An optimal algorithm for finding the longest palindromic substring in linear time. Invented by Glenn K. Manacher in 1975, it cleverly avoids redundant comparisons by utilizing previously computed palindrome information, achieving O(n) time complexity which is optimal for this problem.

String Algorithms
DSA Explorer

Master Data Structures and Algorithms through interactive visualizations and detailed explanations. Our platform helps you understand complex concepts with clear examples and real-world applications.

Quick Links

  • About DSA Explorer
  • All Algorithms
  • Data Structures
  • Contact Support

Legal

  • Privacy Policy
  • Terms of Service
  • Cookie Policy
  • Code of Conduct

Stay Updated

Subscribe to our newsletter for the latest algorithm explanations, coding challenges, and platform updates.

We respect your privacy. Unsubscribe at any time.

© 2026 Momin Studio. All rights reserved.

SitemapAccessibility
v1.0.0