-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathcode_2.cpp
72 lines (64 loc) · 1.32 KB
/
code_2.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
//
// code_2.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 txt, string pat) {
int n = int(txt.length());
int m = int(pat.length());
if (m == 0) {
return (n == 0);
}
int i = 0, j = 0, index_txt = -1,
index_pat = -1;
while (i < n) {
if (txt[i] == pat[j]) {
i++;
j++;
}
else if (j < m && pat[j] == '?') {
i++;
j++;
}
else if (j < m && pat[j] == '*') {
index_txt = i;
index_pat = j;
j++;
}
else if (index_pat != -1) {
j = index_pat + 1;
i = index_txt + 1;
index_txt++;
}
else {
return false;
}
}
while (j < m && pat[j] == '*') {
j++;
}
if (j == m) {
return true;
}
return false;
}
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;
}