-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatch.cpp
More file actions
55 lines (42 loc) · 1.27 KB
/
Copy pathMatch.cpp
File metadata and controls
55 lines (42 loc) · 1.27 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
/*
Poyraz Erdoðan, Created in 01.07.2025
*/
#include "Match.h"
bool Match::match(string pattern, const string text)
{
int textIndex = 0;
int patternIndex = 0;
while (patternIndex < pattern.size())
{
if (patternIndex + 1 < pattern.size() && pattern[patternIndex + 1] == '*')
{
char current = pattern[patternIndex];
int start = textIndex;
// match with greedy
while (textIndex < text.size() && (current == text[textIndex] || current == '.'))
{
textIndex++;
}
// backtrack
for (int i = textIndex; i >= start; i--)
{
if (match(pattern.substr(patternIndex + 2), text.substr(i)))
{
return true;
}
// unsigned wraparound
if (i == 0) break;
}
return false;
}
// . or a
if (textIndex < text.size() && (pattern[patternIndex] == text[textIndex] || pattern[patternIndex] == '.')) {
patternIndex++;
textIndex++;
}
else {
return false;
}
}
return text.size() == textIndex;
}