Wildcard Matching
Problem Statement :
Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*' where: '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). Example 1: Input: s = "aa", p = "a" Output: false Explanation: "a" does not match the entire string "aa". Example 2: Input: s = "aa", p = "*" Output: true Explanation: '*' matches any sequence. Example 3: Input: s = "cb", p = "?a" Output: false Explanation: '?' matches 'c', but the second letter is 'a', which does not match 'b'. Constraints: 0 <= s.length, p.length <= 2000 s contains only lowercase English letters. p contains only lowercase English letters, '?' or '*'.
Solution :
Solution in C :
bool isMatch(char * s, char * p){
int m = strlen(s), n = strlen(p);
int i = 0, j = 0;
int star_idx = -1, s_idx = -1;
while (i < m) {
if (j < n && (s[i] == p[j] || p[j] == '?')) {
i++;
j++;
} else if (j < n && p[j] == '*') {
star_idx = j;
s_idx = i;
j++;
} else if (star_idx != -1) {
j = star_idx + 1;
i = s_idx + 1;
s_idx++;
} else {
return false;
}
}
while (j < n && p[j] == '*') {
j++;
}
return j == n;
}
Solution in C++ :
class Solution {
public:
bool isMatch(string s, string p) {
int n = s.size();
int m = p.size();
vector<vector<bool>> dp(n+1, vector<bool>(m+1, false));
dp[0][0] = true;
if(p[0] == '*') dp[0][1] = true;
for(int j=2; j<=m; j++){
if(p[j-1] == '*') dp[0][j] = dp[0][j-1];
}
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
if(s[i-1] == p[j-1] || p[j-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] ;
}
}
}
}
return dp[n][m];
}
};
Solution in Java :
// TC -> O(|s|*|p|)
// SC -> O(|s|*|p|)
class Solution {
public boolean isMatch(String s, String p) {
return solve(s,p,0,0,new Boolean[s.length()][p.length()]);
}
private boolean solve(String s,String t,int i,int j,Boolean[][] dp){
if(i==s.length()){
return hasNoLCLeft(t,j);
}
if(j==t.length()){
return i==s.length();
}
if(dp[i][j]!=null){
return dp[i][j];
}
if(Character.isLowerCase(t.charAt(j)) && s.charAt(i)!=t.charAt(j)){
return dp[i][j] = false;
}
if(t.charAt(j)=='*'){
if(solve(s,t,i+1,j,dp)){
return true;
}
if(solve(s,t,i,j+1,dp)){
return true;
}
}
return dp[i][j] = solve(s,t,i+1,j+1,dp)? true: false;
}
private boolean hasNoLCLeft(String t,int j){
for(int i=j;i<t.length();i++){
if(t.charAt(i)!='*'){
return false;
}
}
return true;
}
}
Solution in Python :
# DP
def isMatch(self, s: str, p: str) -> bool:
dp = [[False]*(len(p)+1) for i in range(len(s)+1)]
dp[0][0] = True
for j in range(1,len(p)+1):
if p[j-1] == '*':
dp[0][j] = dp[0][j-1]
for i in range(1,len(s)+1):
for j in range(1,len(p)+1):
dp[i][j] = (p[j-1] in [s[i-1],'?','*'] and dp[i-1][j-1]) or (p[j-1] == '*' and (dp[i][j-1] or dp[i-1][j]))
return dp[len(s)][len(p)]
View More Similar Problems
Down to Zero II
You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations N on in each move: 1: If we take 2 integers a and b where , N = a * b , then we can change N = max( a, b ) 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0. Input Format The first line contains the integer Q.
View Solution →Truck Tour
Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0 to (N-1) (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump. Initially, you have a tank of infinite capacity carrying no petr
View Solution →Queries with Fixed Length
Consider an -integer sequence, . We perform a query on by using an integer, , to calculate the result of the following expression: In other words, if we let , then you need to calculate . Given and queries, return a list of answers to each query. Example The first query uses all of the subarrays of length : . The maxima of the subarrays are . The minimum of these is . The secon
View Solution →QHEAP1
This question is designed to help you get a better understanding of basic heap operations. You will be given queries of types: " 1 v " - Add an element to the heap. " 2 v " - Delete the element from the heap. "3" - Print the minimum of all the elements in the heap. NOTE: It is guaranteed that the element to be deleted will be there in the heap. Also, at any instant, only distinct element
View Solution →Jesse and Cookies
Jesse loves cookies. He wants the sweetness of all his cookies to be greater than value K. To do this, Jesse repeatedly mixes two cookies with the least sweetness. He creates a special combined cookie with: sweetness Least sweet cookie 2nd least sweet cookie). He repeats this procedure until all the cookies in his collection have a sweetness > = K. You are given Jesse's cookies. Print t
View Solution →Find the Running Median
The median of a set of integers is the midpoint value of the data set for which an equal number of integers are less than and greater than the value. To find the median, you must first sort your set of integers in non-decreasing order, then: If your set contains an odd number of elements, the median is the middle element of the sorted sample. In the sorted set { 1, 2, 3 } , 2 is the median.
View Solution →