Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Wildcard Matching Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Wildcard Matching

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Implement wildcard matching.

In a wildcard string:

  • a-z matches the corresponding character.
  • ? matches any single character.
  • * matches any sequence of characters (incl. empty sequence).

The entire input string should be matched.

The function would take a string s and the wildcard pattern p.

Naive Approach

The naive approach is to use a recursive brute force approach, to check all the possible lengths of both the strings, and all the possible cases of a character being a ‘*’, ‘?’, or not a wildcard character. Recursively we can try out all the possibilities for both the strings, and check if any of the possible choices of characters, allows both the strings to match completely. If yes, we return true, else after trying out all the possibilities we return false. The cases to be considered are: 

  • The current character is not a wildcard character: If the characters in both the strings don't match, we immediately return false. Else, we move one character ahead in both the strings for processing.
  • The current character is a ‘*’: Ignore the current character and move on to the next one, or assign the next character in the string to this wildcard character.
  • The current character is a ‘?’ : Ignore the current character.

Analysis

  • Time Complexity: Exponential
  • Space Complexity: O(1) // If recursion stack space is ignored.

Implementation

C++
bool solve(string s, string p, int n, int m) {
	if(n == s.length() && m == p.length()) {
		return true;
	}
	if(n == s.length()) {
		for(int k = m; k < p.length(); k++) {
			if(p[k] != '*') {
				return false;
			}
		}
		return true;
	}
	if(m == p.length()) {
		return false;
	}
	bool matched = false;
	if(p[m] == '?' || s[n] == p[m]) {
		matched = solve(s, p, n + 1, m + 1);
	}
	else if(p[m] == '*') {
		matched = solve(s, p, n + 1, m) | solve(s, p, n, m + 1);
	}
	return matched;
}
bool isMatch (string s, string p) {
	return solve(s, p, 0, 0);
}
Java
class Solution {
	boolean solve(char[] s, char[] p, int n, int m) {
		if(n == s.length && m == p.length) {
			return true;
		}
		if(n == s.length) {
			for(int k = m; k < p.length; k++) {
				if(p[k] != '*') {
					return false;
				}
			}
			return true;
		}
		if(m == p.length) {
			return false;
		}
		boolean matched = false;
		if(p[m] == '?' || s[n] == p[m]) {
			matched = solve(s, p, n + 1, m + 1);
		}
		else if(p[m] == '*') {
			matched = solve(s, p, n + 1, m) | solve(s, p, n, m + 1);
		}
		return matched;
	}
	boolean isMatch (String s, String p) {
		char[] sToChar = s.toCharArray();
		char[] pToChar = p.toCharArray();
		return solve(sToChar, pToChar, 0, 0);
	}  	
}

Optimal Approach

We observe that in the naive solution, multiple function calls are being raised again and again. We can memoize those recursive function calls using DP to prevent those recursive calls from happening again and again, and reduce the complexity of the problem to polynomial time complexity.

Analysis:

  • Time Complexity: (|s| * |p|)
  • Space Complexity: (|s| * |p|)

Implementation(Recursive)

C++
vector<vector<int>> dp;
bool solve(string s, string p, int n, int m) {
	if(n == s.length() && m == p.length()) {
		return true;
	}
	if(n == s.length()) {
		for(int k = m; k < p.length(); k++) {
			if(p[k] != '*') {
				return false;
			}
		}
		return true;
	}
	if(m == p.length()) {
		return false;
	}
	if(dp[n][m] != -1) {
		return (bool)dp[n][m];
	}
	bool matched = false;
	if(p[m] == '?' || s[n] == p[m]) {
		matched = solve(s, p, n + 1, m + 1);
	}
	else if(p[m] == '*') {
		matched = solve(s, p, n + 1, m) | solve(s, p, n, m + 1);
	}
	return dp[n][m] = matched;
}
bool isMatch (string s, string p) {
	dp.assign(s.length() + 1, vector<int> (p.length() + 1, -1));
	return solve(s, p, 0, 0);
}
Java
class Solution {
	Boolean[][] dp;
	boolean solve(char[] s, char[] p, int n, int m) {
		if(n == s.length && m == p.length) {
			return true;
		}
		if(n == s.length) {
			for(int k = m; k < p.length; k++) {
				if(p[k] != '*') {
					return false;
				}
			}
			return true;
		}
		if(m == p.length) {
			return false;
		}
		if(dp[n][m] != null) {
			return dp[n][m];
		}
		boolean matched = false;
		if(p[m] == '?' || s[n] == p[m]) {
			matched = solve(s, p, n + 1, m + 1);
		}
		else if(p[m] == '*') {
			matched = solve(s, p, n + 1, m) | solve(s, p, n, m + 1);
		}
		return dp[n][m] = matched;
	}
	boolean isMatch (String s, String p) {
		char[] sToChar = s.toCharArray();
		char[] pToChar = p.toCharArray();
		dp = new Boolean[s.length() + 1][p.length() + 1];
		return solve(sToChar, pToChar, 0, 0);
	}  	
}

Implementation(Iterative)

C++
bool isMatch (string s, string p) {
	int n = s.size();
	int m = p.size();
	bool dp[n + 1][m + 1];
	memset(dp, false, sizeof(dp));
	dp[0][0] = true;
	for(int i = 1; i <= m; i++) {
		if(p[i - 1] == '*') {
			dp[0][i] = dp[0][i - 1];
		}
		else {
			dp[0][i] = false;
		}
	}
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= m; j++) {
			if(p[j - 1] == '?' || (p[j - 1] == s[i - 1])) {
				dp[i][j] = dp[i - 1][j - 1];
			}
			else if(p[j - 1] == '*') {
				dp[i][j] = dp[i - 1][j] | dp[i][j - 1];
			}
			else {
				dp[i][j] = false;
			}
		}
	}
    return dp[n][m];
}
Java
class Solution {
	boolean isMatch (String s, String p) {
	   	int n = s.length();
		int m = p.length();
		boolean[][] dp = new boolean[n + 1][m + 1];
		dp[0][0] = true;
		for(int i = 1; i <= m; i++) {
			if(p.charAt(i - 1) == '*') {
				dp[0][i] = dp[0][i - 1];
			}
			else {
				dp[0][i] = false;
			}
		}
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= m; j++) {
				if(p.charAt(j - 1) == '?' || (p.charAt(j - 1) == s.charAt(i - 1))) {
					dp[i][j] = dp[i - 1][j - 1];
				}
				else if(p.charAt(j - 1) == '*') {
					dp[i][j] = dp[i - 1][j] | dp[i][j - 1];
				}
				else {
					dp[i][j] = false;
				}
			}
		}
		return dp[n][m];
	}
}
Related Content
Best Time to Buy and Sell Stocks
Best Time to Buy and Sell Stocks II
Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock IV
Climbing Stairs
Coin Change
Collect Jewels
Decode Ways
Edit Distance (Levenshtein Distance)
Minimum Egg Drops
Longest Common Subsequence (LCS)
Longest Increasing Subsequence (LIS)
Longest Palindromic Substring (LPS)
Maximum path sum in matrix
Maximum Product Subarray
Maximum Sum Increasing Subsequence
Palindrome Partitioning
Palindrome Partitioning 2
Regular Expression Matching
Rod Cutting
Subset Sum
Subset Sum 2
Trapping Rain Water
Unique Paths
Word Break
Word Break - II
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet