-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathcode_1.cpp
61 lines (54 loc) · 1.34 KB
/
code_1.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
//
// code_1.cpp
// Algorithm
//
// Created by Mohd Shoaib Rayeen on 23/11/18.
// Copyright © 2018 Shoaib Rayeen. All rights reserved.
//
#include <iostream>
#include <string>
using namespace std;
bool isMatch(string str, string pattern) {
int n = int(str.length());
int m = int(pattern.length());
if (m == 0) {
return (n == 0);
}
bool lookup[n + 1][m + 1];
memset(lookup, false, sizeof(lookup));
lookup[0][0] = true;
for (int j = 1; j <= m; j++) {
if (pattern[j - 1] == '*') {
lookup[0][j] = lookup[0][j - 1];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (pattern[j - 1] == '*') {
lookup[i][j] = lookup[i][j - 1] ||lookup[i - 1][j];
}
else if (pattern[j - 1] == '?' || str[i - 1] == pattern[j - 1]) {
lookup[i][j] = lookup[i - 1][j - 1];
}
else {
lookup[i][j] = false;
}
}
}
return lookup[n][m];
}
int main() {
string str;
string pattern;
cout << "\nEnter Text\t:\t";
cin >> str;
cout << "\nEnter Pattern\t:\t";
cin >> pattern;
if( isMatch(str, pattern)) {
cout << "\nPattern Matches\n";
}
else {
cout << "\nPattern does not Match\n";
}
return 0;
}