
- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- Algorithm
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- DSA - Greedy Algorithms
- DSA - Divide and Conquer
- DSA - Dynamic Programming
- Data Structures
- DSA - Data Structure Basics
- DSA - Array Data Structure
- Stack & Queue
- DSA - Stack
- DSA - Expression Parsing
- DSA - Queue
- Searching Techniques
- DSA - Linear Search
- DSA - Binary Search
- DSA - Interpolation Search
- DSA - Hash Table
- Sorting Techniques
- DSA - Sorting Algorithms
- DSA - Bubble Sort
- DSA - Insertion Sort
- DSA - Selection Sort
- DSA - Merge Sort
- DSA - Shell Sort
- DSA - Quick Sort
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Spanning Tree
- DSA - Heap
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion
Wildcard Pattern Matching
For this problem, one main string and another wildcard patterns are given. In this algorithm, it will check whether the wildcard pattern is matching with the main text or not.
The wildcard pattern may contain letters or ‘*’ or ‘?’ Symbols. The ‘?’ Is used to match a single character and ‘*’ is used to match the sequence of characters including empty space.
When the character is ‘*’: We can ignore the star character and move to check next characters in the pattern.
When the next character is ‘?’, then we can ignore only the current character in the text, and check for the next character in pattern and text.
When the pattern character is other than ‘*’ and ‘?’, then if the current character of pattern and text is matching, then only move further.
Input and Output
Input: The main string and the wildcard pattern. Main String “Algorithm” Pattern “A*it?m” Output: The pattern matched.
Algorithm
wildcardMatch(text, pattern)
Input: The main text and the pattern.
Output: True when wildcard patterns matched for the main text.
Begin n := length of the text m := length of pattern if m = 0, then return 0 if n = 0, otherwise return 1 i := 0, j := 0 while i < n, do if text[i] == pattern[i], then increase i by 1 increase j by 1 else if j < m and pattern[j] is ? mark, then increase i by 1 increase j by 1 else if j < m and pattern[j] is * symbol, then textPointer := i patPointer := j increase j by 1 else if patPointer is already updated, then j := patPointer + 1 i := textPinter + 1 increase textPointer by 1 else return false done while j < m and pattern[j] = * symbol, do increase j by 1 done if j = m, then return true return false End
Example
#include<iostream> using namespace std; bool wildcardMatch(string text, string pattern) { int n = text.size(); int m = pattern.size(); if (m == 0) //when pattern is empty return (n == 0); int i = 0, j = 0, textPointer = -1, pattPointer = -1; while (i < n) { if (text[i] == pattern[j]) { //matching text and pattern characters i++; j++; }else if (j < m && pattern[j] == '?') { //as ? used for one character i++; j++; }else if (j < m && pattern[j] == '*') { //as * used for one or more character textPointer = i; pattPointer = j; j++; }else if (pattPointer != -1) { j = pattPointer + 1; i = textPointer + 1; textPointer++; }else return false; } while (j < m && pattern[j] == '*') { j++; //j will increase when wildcard is * } if (j == m) { //check whether pattern is finished or not return true; } return false; } int main() { string text; string pattern; cout << "Enter Text: "; cin >> text; cout << "Enter wildcard pattern: "; cin >> pattern; if (wildcardMatch(text, pattern)) cout << "Pattern Matched." << endl; else cout << "Pattern is not matched" << endl; }
Output
Enter Text: Algorithm Enter wildcard pattern: A*it?m Pattern Matched.
- Related Articles
- Wildcard Matching in Python
- Wildcard matching of string JavaScript
- Matching strings with a wildcard in C#
- Unix filename pattern matching in Python
- Pattern matching in C# with Regex
- Pattern matching in Python with Regex
- Lua pattern matching vs regular expression
- Unix filename pattern matching in Python (fnmatch)
- What is Pattern Matching in C# 7.0?
- What is MySQL REGEXP operator and how it handles pattern matching?
- MySQL pattern matching 3 or more “a's” in name?
- Which package is used for pattern matching with regular expressions in java?
- Print all words matching a pattern in CamelCase Notation Dictionary in C++
- Program to check regular expression pattern is matching with string or not in Python
- Golang Program to get the List of filenames matching the specified wild card pattern
