-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecember28_wildcardmatch.cpp
103 lines (89 loc) · 2.56 KB
/
December28_wildcardmatch.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
Author : Manas Rawat
Date : 28/12/2023
Problem : Wildcard string matching
Difficulty : Hard
Problem Link : https://www.geeksforgeeks.org/problems/wildcard-string-matching1126/1
Video Solution : https://youtu.be/XbbHvYi3Qdo
*/
//{ Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// User function template for C++
class Solution{
public:
// bool match(string wild, string pattern)
// {
// int n = wild.size();
// int m = pattern.size();
// vector<vector<int>> dp(n + 1, vector<int> (m + 1, 0));
// dp[n][m] = 1;
// for(int i = n - 1; i > -1; i--){
// for(int j = m - 1; j > -1; j--){
// if(wild[i] == '?')
// dp[i][j] = dp[i + 1][j + 1];
// else if(wild[i] == '*'){
// if(i == n - 1)
// dp[i][j] = 1;
// else
// dp[i][j] = dp[i + 1][j] or dp[i][j + 1];
// }
// else{
// if(wild[i] == pattern[j])
// dp[i][j] = dp[i + 1][j + 1];
// else
// dp[i][j] = 0;
// }
// }
// }
// return dp[0][0];
// }
bool match(string wild, string pattern)
{
int n = wild.length(), m = pattern.length();
vector<vector<int>> arr(n + 1, vector<int>(m + 1, 0));
arr[n][m] = 1;
for (int i = n - 1; i >= 0; i--) {
for (int j = m - 1; j >= 0; j--) {
if (wild[i] == pattern[j] && arr[i + 1][j + 1] == 1) {
arr[i][j] = 1;
}
if (wild[i] == '?' && arr[i + 1][j + 1] == 1) {
arr[i][j] = 1;
}
if (wild[i] == '*' && (arr[i + 1][j + 1] == 1 || arr[i + 1][j] == 1)) {
arr[i][j]=1; while (j-- > 0) {
arr[i][j] = 1;
}
}
}
}
for(int i=0;i<=n;i++)
{
for(int j=0;j<=m;j++)
cout<<arr[i][j]<<" ";
cout<<"\n";
}
return arr[0][0] == 1;
}
};
//{ Driver Code Starts.
int main()
{
int t;
cin>>t;
while(t-->0)
{
string wild, pattern;
cin>>wild>>pattern;
Solution ob;
bool x=ob.match(wild, pattern);
if(x)
cout<<"Yes\n";
else
cout<<"No\n";
}
return 0;
}
// } Driver Code Ends