A comprehensive educational implementation of the naive string pattern matching algorithm, available in multiple programming languages and formats.
- How computers search for patterns in text
- Character-by-character comparison logic
- The sliding window technique
- Time complexity analysis (O(nΓm))
- Edge case handling
- Algorithm visualization
File: pattern_search.py
A detailed terminal-based implementation with colorful output and comprehensive explanations.
Features:
- β Step-by-step character comparisons
- β Visual representation with ASCII art
- β 10 automated test cases
- β Interactive mode
- β Educational explanations
- β Bonus features (case-insensitive, count occurrences, etc.)
Usage:
python pattern_search.pyFile: pattern_search_gui.py
Beautiful interactive GUI with animated visualizations using tkinter.
Features:
- β Visual canvas with color-coded character boxes
- β Real-time animated search process
- β Adjustable animation speed (0.1x to 2.0x)
- β Case-sensitive/insensitive toggle
- β Pre-loaded example test cases
- β Live output with syntax highlighting
- β Status bar with progress updates
Usage:
python pattern_search_gui.pyRequirements:
- Python 3.x
- tkinter (usually included with Python)
File: PatternSearch.java
A comprehensive Java implementation with ANSI color support for terminals.
Features:
- β Object-oriented design
- β Colorful terminal output
- β Multiple helper methods
- β 10 comprehensive test cases
- β Interactive mode
- β Case-insensitive search
- β Modern Java features (text blocks, ArrayList, etc.)
Usage:
# Compile
javac PatternSearch.java
# Run
java PatternSearchRequirements:
- Java 11 or higher (for text blocks)
File: PatternSearchGUI.java
Professional GUI application with JavaFX featuring smooth animations and modern design.
Features:
- β Modern GUI with JavaFX
- β Canvas-based character visualization
- β Color-coded boxes (green=match, red=no match, blue=checking)
- β Background threading for smooth performance
- β Adjustable animation speed slider
- β Pre-loaded examples in popup window
- β Status bar with live updates
- β Start/Stop/Clear controls
Setup Required: See SETUP_JAVAFX.md for detailed installation instructions.
Quick Start (if JavaFX is installed):
# Compile
javac --module-path "path/to/javafx/lib" --add-modules javafx.controls,javafx.graphics PatternSearchGUI.java
# Run
java --module-path "path/to/javafx/lib" --add-modules javafx.controls,javafx.graphics PatternSearchGUIRequirements:
- Java 11 or higher
- JavaFX SDK 11 or higher
- Want quick results? β Use Python Terminal Version (
pattern_search.py) - Want visual learning? β Use Python GUI Version (
pattern_search_gui.py) - Learning Java? β Use Java Terminal Version (
PatternSearch.java) - Want professional GUI? β Use JavaFX GUI Version (needs setup)
All versions support searching for patterns like:
Text: "banana"
Pattern: "ana"
Result: Found at positions [1, 3] (overlapping matches!)
Text: "hello world"
Pattern: "wor"
Result: Found at position [6]
Text: "Mississippi"
Pattern: "issi"
Result: Found at positions [1, 4]
- Start at position 0 of the text
- Compare pattern with text at current position
- Check each character one by one
- If all match β Record this position!
- If any don't match β Move to next position
- Slide to the next position (move one character right)
- Repeat until all positions are checked
- Return all positions where matches were found
Text: b a n a n a
Pattern: a n a
Position 0: [b a n] a n a β No match (b β a)
Position 1: b [a n a] n a β Match! β
Position 2: b a [n a n] a β No match (n β a)
Position 3: b a n [a n a] β Match! β
Result: Matches at positions [1, 3]
- Best Case: O(n) - pattern doesn't match first character anywhere
- Worst Case: O(n Γ m) - pattern almost matches everywhere
- Space Complexity: O(1) - only stores match positions
Where:
- n = length of text
- m = length of pattern
- β Basic successful search
- β Overlapping matches
- β Pattern not found
- β Repeated characters
- β Empty text/pattern
- β Pattern longer than text
- β Single character search
- β Exact match
- β Case-insensitive search
- β Multiple occurrences
- π€ Case-insensitive search - Find matches regardless of case
- π’ Count occurrences - How many times pattern appears
- π First/Last occurrence - Find specific positions
- π¨ Visual highlighting - See matches in real-time
- β±οΈ Animation speed control - Learn at your own pace
Where in the string we're currently looking (0-based indexing).
Checking if two characters are equal: text[i] == pattern[j]
Moving the pattern one position at a time across the text.
Outer loop: positions in text
Inner loop: characters in pattern
Stop comparing when a mismatch is found (optimization).
After mastering this naive approach, explore more efficient algorithms:
- KMP (Knuth-Morris-Pratt) - O(n + m) time complexity
- Boyer-Moore - Often faster in practice, works backwards
- Rabin-Karp - Uses hashing for pattern matching
- Aho-Corasick - For multiple pattern matching
- Regular Expressions - Powerful pattern matching with special syntax
- Make sure tkinter is installed:
python -m tkinter - Try reinstalling Python with tkinter support
- Your terminal might not support ANSI colors
- Try a different terminal (Windows Terminal, iTerm2, etc.)
- See SETUP_JAVAFX.md for detailed setup
- Consider using the terminal version or Python GUI as alternatives
Strin/
β
βββ pattern_search.py # Python terminal version
βββ pattern_search_gui.py # Python GUI version (tkinter)
βββ PatternSearch.java # Java terminal version
βββ PatternSearchGUI.java # JavaFX GUI version
βββ README.md # This file
βββ SETUP_JAVAFX.md # JavaFX setup instructions
This is an educational project. Feel free to:
- Add more test cases
- Implement more efficient algorithms
- Create versions in other languages
- Improve visualizations
- Add more educational features
This project is created for educational purposes. Feel free to use and modify for learning!
After completing this tutorial, you should be able to:
β
Understand how pattern matching works fundamentally
β
Write your own pattern search algorithm from scratch
β
Analyze time and space complexity
β
Handle edge cases in algorithms
β
Visualize algorithm execution
β
Compare different algorithmic approaches
β
Apply the sliding window technique to other problems
Happy Learning! π
Start with the version that matches your skill level and gradually explore the others!