|
| 1 | +class Solution { |
| 2 | +public: |
| 3 | + bool recur(string& s,string& p, int& m, int& n, int i, int j) { |
| 4 | + if(i>=m && j>=n) |
| 5 | + return true; |
| 6 | + if(i<m && j<n) { |
| 7 | + if(j+1<n) { |
| 8 | + if(p[j+1] == '*') { |
| 9 | + return (recur(s,p,m,n,i,j+2) || ((s[i]==p[j] || p[j]=='.') ? recur(s,p,m,n,i+1,j) : false)); |
| 10 | + } |
| 11 | + return ((s[i]==p[j] || p[j]=='.') ? recur(s,p,m,n,i+1,j+1) : false); |
| 12 | + } |
| 13 | + if(s[i] == p[j] || p[j]=='.') |
| 14 | + return (i == m-1); |
| 15 | + } |
| 16 | + if(i>=m && j<n) { |
| 17 | + return isConsumable(p,j,n,s[m-1]); |
| 18 | + } |
| 19 | + return false; |
| 20 | + } |
| 21 | + bool isConsumable(string& p, int j,int n,char a) { |
| 22 | + bool alt=false; |
| 23 | + stack <char> s1; |
| 24 | + if(p[j] == '*') { |
| 25 | + alt = true; |
| 26 | + j++; |
| 27 | + } |
| 28 | + while(j<n){ |
| 29 | + if(p[j] != '*') { |
| 30 | + s1.push(p[j]); |
| 31 | + } else { |
| 32 | + if(s1.empty()) |
| 33 | + return false; |
| 34 | + s1.pop(); |
| 35 | + } |
| 36 | + j++; |
| 37 | + } |
| 38 | + if(alt && s1.size() == 1 && s1.top() == a) { |
| 39 | + return true; |
| 40 | + } |
| 41 | + if(s1.empty()) |
| 42 | + return true; |
| 43 | + return false; |
| 44 | + } |
| 45 | + |
| 46 | + bool isMatch(string s, string p) { |
| 47 | + int m = s.length(); |
| 48 | + int n = p.length(); |
| 49 | + char prev = ' '; |
| 50 | + int s_txt = 0; |
| 51 | + int p_txt = 0; |
| 52 | + int count = 0; |
| 53 | + bool f = false; |
| 54 | + int i=0,j=0,c=0; |
| 55 | + if (!m && !n) |
| 56 | + return true; |
| 57 | + if(!n || p[0] == '*') { |
| 58 | + cout<<"\nExiting due to pattern length"; |
| 59 | + return false; |
| 60 | + } |
| 61 | + return recur(s,p,m,n,0,0); |
| 62 | + } |
| 63 | +}; |
| 64 | + |
| 65 | +string stringToString(string input) { |
| 66 | + assert(input.length() >= 2); |
| 67 | + string result; |
| 68 | + for (int i = 1; i < input.length() -1; i++) { |
| 69 | + char currentChar = input[i]; |
| 70 | + if (input[i] == '\\') { |
| 71 | + char nextChar = input[i+1]; |
| 72 | + switch (nextChar) { |
| 73 | + case '\"': result.push_back('\"'); break; |
| 74 | + case '/' : result.push_back('/'); break; |
| 75 | + case '\\': result.push_back('\\'); break; |
| 76 | + case 'b' : result.push_back('\b'); break; |
| 77 | + case 'f' : result.push_back('\f'); break; |
| 78 | + case 'r' : result.push_back('\r'); break; |
| 79 | + case 'n' : result.push_back('\n'); break; |
| 80 | + case 't' : result.push_back('\t'); break; |
| 81 | + default: break; |
| 82 | + } |
| 83 | + i++; |
| 84 | + } else { |
| 85 | + result.push_back(currentChar); |
| 86 | + } |
| 87 | + } |
| 88 | + return result; |
| 89 | +} |
| 90 | + |
| 91 | +string boolToString(bool input) { |
| 92 | + return input ? "True" : "False"; |
| 93 | +} |
| 94 | + |
| 95 | +int main() { |
| 96 | + string line; |
| 97 | + while (getline(cin, line)) { |
| 98 | + string s = stringToString(line); |
| 99 | + getline(cin, line); |
| 100 | + string p = stringToString(line); |
| 101 | + |
| 102 | + bool ret = Solution().isMatch(s, p); |
| 103 | + |
| 104 | + string out = boolToString(ret); |
| 105 | + cout << out << endl; |
| 106 | + } |
| 107 | + return 0; |
| 108 | +} |
0 commit comments