Skip to content

NarendraYSF/String-Search-Algorithm

Repository files navigation

πŸ” String Pattern Search Algorithm - Educational Tool

A comprehensive educational implementation of the naive string pattern matching algorithm, available in multiple programming languages and formats.

πŸ“š What You'll Learn

  • 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

🎯 Available Versions

1. Python Terminal Version

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.py

2. Python GUI Version

File: 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.py

Requirements:

  • Python 3.x
  • tkinter (usually included with Python)

3. Java Terminal Version

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 PatternSearch

Requirements:

  • Java 11 or higher (for text blocks)

4. JavaFX GUI Version

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 PatternSearchGUI

Requirements:

  • Java 11 or higher
  • JavaFX SDK 11 or higher

πŸš€ Quick Start Guide

Choose Your Version:

  1. Want quick results? β†’ Use Python Terminal Version (pattern_search.py)
  2. Want visual learning? β†’ Use Python GUI Version (pattern_search_gui.py)
  3. Learning Java? β†’ Use Java Terminal Version (PatternSearch.java)
  4. Want professional GUI? β†’ Use JavaFX GUI Version (needs setup)

Example Usage:

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]

πŸ“– How the Algorithm Works

The Naive/Brute-Force Approach:

  1. Start at position 0 of the text
  2. 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
  3. Slide to the next position (move one character right)
  4. Repeat until all positions are checked
  5. Return all positions where matches were found

Visual Example:

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]

Time Complexity:

  • 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

πŸŽ“ Educational Features

Test Cases Included:

  1. βœ… Basic successful search
  2. βœ… Overlapping matches
  3. βœ… Pattern not found
  4. βœ… Repeated characters
  5. βœ… Empty text/pattern
  6. βœ… Pattern longer than text
  7. βœ… Single character search
  8. βœ… Exact match
  9. βœ… Case-insensitive search
  10. βœ… Multiple occurrences

Bonus Features:

  • πŸ”€ 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

🌟 Key Concepts Explained

1. Index/Position

Where in the string we're currently looking (0-based indexing).

2. Character Comparison

Checking if two characters are equal: text[i] == pattern[j]

3. Sliding Window

Moving the pattern one position at a time across the text.

4. Nested Loops

Outer loop: positions in text
Inner loop: characters in pattern

5. Early Termination

Stop comparing when a mismatch is found (optimization).

πŸ’‘ Next Steps

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

πŸ› Troubleshooting

Python GUI won't start

  • Make sure tkinter is installed: python -m tkinter
  • Try reinstalling Python with tkinter support

Java colors not showing

  • Your terminal might not support ANSI colors
  • Try a different terminal (Windows Terminal, iTerm2, etc.)

JavaFX errors

  • See SETUP_JAVAFX.md for detailed setup
  • Consider using the terminal version or Python GUI as alternatives

πŸ“ Project Structure

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

🀝 Contributing

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

πŸ“ License

This project is created for educational purposes. Feel free to use and modify for learning!

🎯 Learning Outcomes

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!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published