Algorithms Tutorial

Searching Algorithms

Dynamic Programming

Graph Algorithms

Backtracking Algorithms


Operating System Algorithms

Miscellaneous Topics

Wild card matching problem using Dynamic programming (DP)

Here, we are going to solve the wild chard matching problem using dynamic programming (DP) using C++ program.
Submitted by Ritik Aggarwal, on December 14, 2018

Problem statement

You are given two strings (one query and other pattern). You have to find whether the pattern matches to query or not. Print '1' if matches otherwise '0'. Some rules for matching:

  1. A "*" can match any substring (blank spaces also).
  2. A "?" can match any single character (excluding blank spaces).

Constraints: (length of pattern) * (length of query) < 100000000




For the sample Input1,

'*' can be replaced with 'bsb' and '?' can be replaced with 's'. Thus, both pattern and query matches so the output is 1.

For the sample input2,

'*' can be replaced with 'dv' but we cannot replace '?' with 'vd' as it can replace only one character. Thus, pattern and query cannot match so the output is 0.


  1. STEP-1: Create a 2D dp matrix where jth cell of ith row represent whether the pattern (from ith index to the end) and the query (from jth index to end) matches or not.
  2. STEP-2: If pattern is the empty and the query is non-empty then it does not matches. So initializing dp matrix with false for the cell corresponding to this situation.
  3. STEP-3: if the query is empty and the pattern contains all '*' (pattern is something like ******) then put these corresponding values true.
  4. STEP-4: Start filling the dp matrix from right to left and bottom to top.
  5. STEP-5: If the ith character of pattern matches with jth character of query or the ith character is '?' then we can simply remove that ith character from pattern and jth character from query and check the right-bottom box and put its value in our current cell.
  6. STEP-6: if ith character of the pattern is '*' then check the right adjacent box(this box corresponds to situation when we remove the jth character from query) , bottom box(this box corresponds to situation when we remove the ith character from pattern) and the right-bottom adjacent(this box corresponds to situation when we remove the jth character from query and remove ith character from the pattern) value. If any of them is true then put true in current cell.
  7. STEP-7: Otherwise, set cells to false
  8. STEP-8: return the answer of i=0 and j=0 as they represent full query and pattern.

The time complexity of the above code is O(length of pattern * length of query).

C++ program for wild card matching problem using Dynamic programming

#include <iostream>
using namespace std;

bool wildCard(string pattern, string s){
	bool dp[pattern.length() + 1][s.length() + 1] = {false};
	// Intialization of few values necessary for the dp relations
	// Case 1. pattern is a blank string
	// In this case we only have to initialize right-bottom 
	// corner value to true as pattern is also empty and s is also empty
	dp[pattern.length()][s.length()] = true;
	// Case 2. s is empty 
	for(int row = pattern.length() - 1;row >= 0;row--){
		// if pattern contains only stars and s is empty only then it is true
		if(pattern[row] == '*' && dp[row + 1][s.length()] == true){
			dp[row][s.length()] = true;
			dp[row][s.length()] = false;
	// filling the dp matrix(right to left and bottom to top) 
	for(int row = pattern.length() -1;row >= 0;row--){
		for(int col = s.length() - 1;col>=0;col--){
			// if the characters of pattern and s is equal or patterns 
			// rowth character is a ? then just pick up rigth bottom value
			if((pattern[row] == s[col]) || pattern[row] == '?'){
				dp[row][col] = dp[row + 1][col + 1];
			// if there is a star then take the || of the bottom 
			// adjacent, right adjacent and rigth-bottom value;
			else if(pattern[row] == '*'){
				dp[row][col] = (dp[row+1][col] || dp[row+1][col+1] || dp[row][col+1]);
			// otherwise store false
				dp[row][col] = false;
	return dp[0][0];  
// driver programme for the code
int main() {
	string s, pattern;
	cin >> s;
	cin >> pattern;
	cout << s << endl;
	cout << pattern << endl;
	cout<<wildCard(pattern, s);
	return 0;



Related Tutorials

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.