Prepin
Log in

Given an input string (s) and a pattern (p), create a function for wildcard pattern matching that recognizes '?' and '*' as follows: '?' corresponds to any single character. '*' corresponds to any sequence of characters, including an empty sequence. The entire input string must match the pattern, not just a portion of it.

Approach / Explanation

This inquiry is a well-known challenge in dynamic programming and string manipulation within the fields of Computer Science and Algorithms. You are tasked with creating an algorithm for wildcard pattern matching that accommodates the characters '?' and '*'. '?' can represent any single character. '*' can represent any sequence of characters, including an empty sequence. The challenge you need to address is whether the provided input string (s) can be completely matched (not partially) by the pattern string (p) based on these matching criteria. Important points to consider include: Are there limitations on the types of input characters? How to interpret and manage '?' and '*' How to apply dynamic programming to solve this issue?

Suggested Answer

A simple method to address this issue is through Dynamic Programming. The general outline of the algorithm is as follows: Construct a 2D boolean matrix dp such that dp[i][j] is True if the first i characters of the given string correspond to the first j characters of the pattern. The first row represents an empty string. Therefore, dp[0][j] (for j>0 ) will be False , as an empty string can only match a pattern that consists solely of '*'. The first column signifies an empty pattern. dp[i][0] will only be True if the input string is also empty. For the remaining cells in the matrix, we will use the following rules to fill it: If the character in the pattern is a '?', or matches the character in the string, then dp[i][j] = dp[i - 1][j - 1] . If the character in the pattern is '*', then dp[i][j] = dp[i - 1][j] (treating the '*' as matching the current character in the string and ignoring the rest) or dp[i][j - 1] (ignoring the '*' and continuing with previous matches). Finally, dp[s.length()][p.length()] will provide the result, indicating whether the entire string can be matched by the pattern. In Python, this implementation would look like: def isMatch ( s , p ) : dp = [ [ False ] * ( len ( p ) + 1 ) for _ in range ( len ( s ) + 1 ) ] dp [ - 1 ] [ - 1 ] = True for i in range ( len ( s ) , - 1 , - 1 ) : for j in range ( len ( p ) - 1 , - 1 , - 1 ) : match = i < len ( s ) and ( s [ i ] == p [ j ] or p [ j ] == '?' ) if p [ j ] == '*' : dp [ i ] [ j ] = dp [ i ] [ j + 1 ] or i < len ( s ) and dp [ i + 1 ] [ j ] else : dp [ i ] [ j ] = match and dp [ i + 1 ] [ j + 1 ] return dp [ 0 ] [ 0 ]

Alternative Answer

Another approach to address this issue is through the use of a recursive function, though it is not as efficient as dynamic programming. This technique employs two pointers: one for the string and the other for the pattern. If the current character in the pattern is '?' or if the character in the string matches the one in the pattern, move both pointers forward. If the current character in the pattern is ' ', we keep a backup of the current positions of the string and pattern, advance the pattern pointer, and continue matching. If there is a mismatch, we revert the string to the next position using the backup and attempt to match again with ' '. When the end of the string is reached, verify the remainder of the pattern for any ' ' characters. If all remaining characters in the pattern are ' ', the function returns true; otherwise, it returns false. Below is the implementation in Python: def isMatch ( s , p ) : s_cur = p_cur = backup_s = backup_p = 0 while s_cur < len ( s ) : if p_cur < len ( p ) and ( s [ s_cur ] == p [ p_cur ] or p [ p_cur ] == '?' ) : s_cur += 1 p_cur += 1 elif p_cur < len ( p ) and p [ p_cur ] == '*' : backup_s = s_cur backup_p = p_cur + 1 p_cur += 1 elif backup_p != 0 : s_cur = backup_s + 1 backup_s += 1 p_cur = backup_p else : return False while p_cur < len ( p ) and p [ p_cur ] == '*' : p_cur += 1 if p_cur == len ( p ) : return True return False While this algorithm can effectively resolve the issue, it has a worst-case time complexity of O(SP), where S and P represent the lengths of the string and the pattern, respectively. In contrast, using dynamic programming can reduce the time complexity to O(SP), making it a more efficient solution.

Question Details

Difficulty & Category

Medium
algorithms

Companies

Service Now

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.