|
| 1 | +/* Question-:given 2 strings s1 and s2.We have to match string s1 containing (*,?,characters) |
| 2 | +to string s2 containing only characters,*/ |
| 3 | +// * can contain characters ranging from 0 to size of string |
| 4 | +// ? can contain single string |
| 5 | + |
| 6 | +#include<bits/stdc++.h> |
| 7 | +using namespace std; |
| 8 | + |
| 9 | +//Done with the help of recursion |
| 10 | +bool fun(string &pattern, string &text, int i, int j) |
| 11 | +{ |
| 12 | + if (i < 0 && j < 0) //(Base Case) //T.C-: O(length of string s2*length of string s2) |
| 13 | + return true; //(Base Case) //S.C-: O(length of string s2*length of string s2 + auxilliary stack space of string s1 and s2) |
| 14 | + if (i < 0 && j >= 0) |
| 15 | + return false; |
| 16 | + if (j < 0 && i >= 0) |
| 17 | + { |
| 18 | + for (int i1 = 0; i1 <= i; i1++) |
| 19 | + { |
| 20 | + if (pattern[i1] != '*') //for the edge case if string s2 empty and s1 contains only * |
| 21 | + return false; |
| 22 | + } |
| 23 | + return true; |
| 24 | + } |
| 25 | + if (pattern[i] == text[j] || pattern[i] == '?') |
| 26 | + return fun(pattern, text, i - 1, j - 1); //Having a similiar character and a ? is same.As ? can be replaced to the required character |
| 27 | + if (pattern[i] == '*') |
| 28 | + return fun(pattern, text, i - 1, j) or fun(pattern, text, i, j - 1); //first function symbolizes if the * has more than 1 characters |
| 29 | + return false; //second function symbolizes if the * has only 1 character |
| 30 | +} |
| 31 | +bool wildcardMatching(string pattern, string text) |
| 32 | +{ |
| 33 | + // Write your code here. |
| 34 | + int n = pattern.length(); |
| 35 | + int m = text.length(); |
| 36 | + return fun(pattern, text, n, m); //calling function |
| 37 | +} |
| 38 | +int main() //Main Function |
| 39 | +{ |
| 40 | + string s; |
| 41 | + cin >> s; |
| 42 | + string s1; |
| 43 | + cin >> s1; |
| 44 | + cout << wildcardMatching(s, s1); |
| 45 | +} |
0 commit comments