-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmymatcher.cpp
43 lines (42 loc) · 948 Bytes
/
mymatcher.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
#include "stdafx.h"
#include <string>
#include <unordered_map>
#include <unordered_set>
bool matchString(std::string text) {
static const std::unordered_map<char, int> transitions[] = {
std::unordered_map<char, int>({
{ 'a', 1 },
{ 'b', 2 },
}),
std::unordered_map<char, int>({
{ 'a', 1 },
{ 'b', 3 },
}),
std::unordered_map<char, int>({
{ 'a', 1 },
{ 'b', 2 },
}),
std::unordered_map<char, int>({
{ 'a', 1 },
{ 'b', 4 },
}),
std::unordered_map<char, int>({
{ 'a', 1 },
{ 'b', 2 },
}),
};
static const std::unordered_set<int> finalStates = {
4,
};
int currentState = 0;
for (auto it = text.begin(); it != text.end(); it++) {
auto t = transitions[currentState];
auto nextState = t.find(*it);
if (nextState != t.end()) {
currentState = nextState->second;
} else {
return false;
}
}
return (finalStates.find(currentState) != finalStates.end());
}