-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathdetermine-pattern-matches-string-not.cpp
More file actions
74 lines (58 loc) · 1.52 KB
/
determine-pattern-matches-string-not.cpp
File metadata and controls
74 lines (58 loc) · 1.52 KB
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
#include <iostream>
#include <unordered_map>
using namespace std;
// Function to determine if given pattern matches with a string or not
bool match(string str, int i, string pat, int j, unordered_map<char, string> &map)
{
int n = str.size();
int m = pat.size();
if (n < m)
return false;
// if both pattern and the string reaches end
if (i == n && j == m)
return true;
// if either string or pattern reaches end
if (i == n || j == m)
return false;
//take the curr j as curr
char curr = pat[j];
if (map.find(curr) != map.end())
{
string s = map[curr];
int k = s.size();
//i ke baad agle k characters agar s se match na kare to return false
if (str.substr(i, k).compare(s))
return false;
return match(str, i + k, pat, j + 1, map);
}
for (int k = 1; k <= n - i; k++)
{
map[curr] = str.substr(i, k);
if (match(str, i + k, pat, j + 1, map))
return true;
map.erase(curr);
}
return false;
}
// main function
int main()
{
// input string and pattern
string str = "codesleepcode";
string pat = "XYX";
// create a map to store mappings between the pattern and string
unordered_map<char, string> map;
// check for solution
if (match(str, 0, pat, 0, map))
{
for (auto entry : map)
{
cout << entry.first << ": " << entry.second << endl;
}
}
else
{
cout << "Solution doesn't exist";
}
return 0;
}