# Wildcard Pattern Matcher - LeetCode 44 Solution

## Overview

This project implements a comprehensive solution to the **LeetCode 44 - Wildcard Matching** problem, converted from a Sudoku Solver using similar algorithmic principles. The solution supports both dynamic programming and recursive backtracking approaches.

## Problem Description

Given an input string (`s`) and a pattern (`p`), implement wildcard pattern matching with support for:
- `'?'` - Matches any single character
- `'*'` - Matches any sequence of characters (including empty sequence)

The matching should cover the **entire** input string (not partial).

## Conversion from Sudoku Solver

### Original Sudoku Solver Structure
The original Sudoku solver used:
- **Backtracking algorithm** for constraint satisfaction
- **Grid validation** with constraint checking
- **Recursive exploration** of solution space
- **State management** with counters and statistics

### Wildcard Matcher Adaptation
The conversion maintains similar architectural patterns:

| Sudoku Solver | Wildcard Matcher | Purpose |
|---------------|------------------|---------|
| `SolvePuzzle()` | `IsMatch()` | Main solving function |
| `SolveRecursive()` | `IsMatchRecursiveHelper()` | Recursive exploration |
| `IsNumberSafe()` | `IsValidPattern()` | Constraint validation |
| `FindEmptyCell()` | Pattern preprocessing | State space navigation |
| Grid constraints | Character matching rules | Problem-specific logic |

## Features

### 🚀 **Dual Algorithm Implementation**
- **Dynamic Programming**: O(m×n) time complexity, optimal for most cases
- **Recursive Backtracking**: Similar to Sudoku solver approach, with memoization potential

### 🎯 **Pattern Optimization**
- **Consecutive '*' merging**: Reduces redundant computations
- **Early termination**: Optimized base cases for edge scenarios
- **Memory efficient**: Minimal space overhead

### 📊 **Performance Monitoring**
- **Recursion counting**: Track algorithm depth
- **Backtrack statistics**: Monitor exploration efficiency  
- **DP table sizing**: Memory usage analysis

### 🧪 **Comprehensive Testing**
- **LeetCode test cases**: All official problem examples
- **Edge case coverage**: Empty strings, complex patterns
- **Performance comparison**: DP vs Recursive approaches

## Algorithm Approaches

### 1. Dynamic Programming (Primary)
```cpp
bool IsMatchDP(const std::string& text, const std::string& pattern)
```
- **Time Complexity**: O(m × n) where m = text length, n = pattern length
- **Space Complexity**: O(m × n) for DP table
- **Best for**: Most practical applications, guaranteed optimal performance

### 2. Recursive Backtracking (Alternative)
```cpp
bool IsMatchRecursive(const std::string& text, const std::string& pattern)
```
- **Time Complexity**: O(2^(m+n)) worst case, much better with optimization
- **Space Complexity**: O(m + n) for recursion stack
- **Best for**: Understanding algorithm logic, similar to Sudoku solver approach

## Building and Running

### Prerequisites
- C++17 compatible compiler (g++, clang++)
- Make utility
- Optional: valgrind (memory checking), cppcheck (static analysis)

### Build Commands
```bash
# Build the project
make -f Makefile_wildcard

# Run with automatic tests
make -f Makefile_wildcard run

# Build debug version
make -f Makefile_wildcard debug

# Run tests only
make -f Makefile_wildcard test

# Clean build artifacts
make -f Makefile_wildcard clean
```

### Advanced Usage
```bash
# Memory leak detection
make -f Makefile_wildcard memcheck

# Static code analysis
make -f Makefile_wildcard analyze

# Code formatting
make -f Makefile_wildcard format

# Install system-wide
make -f Makefile_wildcard install
```

## Usage Examples

### Command Line Interface
```bash
./wildcard_matcher
```

### Interactive Mode
```
Enter text to match: hello
Enter wildcard pattern: h*o
Result: MATCH ✅
```

### Programmatic Usage
```cpp
#include "WildcardMatcher.h"

WildcardMatcher matcher;
bool result = matcher.IsMatch("hello", "h*o");  // true
matcher.PrintMatchResult("hello", "h*o", result);
```

## Test Cases

The implementation includes comprehensive test cases from LeetCode:

| Text | Pattern | Expected | Description |
|------|---------|----------|-------------|
| `"aa"` | `"a"` | `false` | Pattern too short |
| `"aa"` | `"*"` | `true` | '*' matches everything |
| `"cb"` | `"?a"` | `false` | Second char mismatch |
| `"adceb"` | `"*a*b*"` | `true` | Multiple '*' wildcards |
| `"acdcb"` | `"a*c?b"` | `false` | '?' position mismatch |

## Performance Characteristics

### Benchmark Results (Typical)
- **Simple patterns**: < 1ms
- **Complex patterns**: 1-10ms  
- **Pathological cases**: Handled efficiently by DP approach
- **Memory usage**: Proportional to input size

### Algorithm Comparison
| Metric | Dynamic Programming | Recursive Backtracking |
|--------|-------------------|----------------------|
| **Consistency** | Always O(m×n) | Variable, can be exponential |
| **Memory** | Higher (DP table) | Lower (stack only) |
| **Readability** | Moderate | High (similar to Sudoku) |
| **Optimization** | Built-in | Requires memoization |

## Architecture Highlights

### Class Design (Following C++ Style Guide)
```cpp
class WildcardMatcher {
private:
    int mRecursionCount;     // 'm' prefix for member variables
    int mBacktrackCount;     // Consistent with Sudoku solver style
    
public:
    bool IsMatch();          // Clear verb-based method names
    void PrintMatchResult(); // Descriptive action names
};
```

### Error Handling
- **Input validation**: Pattern and text verification
- **Graceful degradation**: Fallback to alternative algorithms
- **Informative output**: Detailed error messages and statistics

## Future Enhancements

### Potential Optimizations
1. **Memoization**: Add caching to recursive approach
2. **Space optimization**: Reduce DP table to O(n) space
3. **Pattern compilation**: Pre-process patterns for repeated use
4. **Parallel processing**: Multi-threaded pattern matching

### Extended Features
1. **Case-insensitive matching**: Optional flag for case handling
2. **Unicode support**: Extended character set handling
3. **Regular expression**: Expand to full regex support
4. **Batch processing**: Multiple pattern matching

## License

This project is licensed under the Apache License 2.0. See the license headers in source files for details.

## Acknowledgments

- **Original Sudoku Solver**: Foundation for backtracking approach
- **LeetCode Problem 44**: Problem specification and test cases
- **Dynamic Programming**: Optimal solution methodology
- **C++ Style Guide**: Consistent coding standards

---

**Note**: This implementation demonstrates how algorithmic patterns from one domain (constraint satisfaction in Sudoku) can be effectively adapted to another domain (string pattern matching), maintaining code quality and architectural consistency.