10. Regular Expression Matching
Runtime: 16 ms, faster than 57.56% of C++ online submissions for Regular Expression Matching.
Memory Usage: 11.5 MB, less than 56.12% of C++ online submissions for Regular Expression Matching.
class Solution {
public:
bool isMatch(string s, string p) {
if (p[0] == '*'){
return true;
}
struct Node
{
char selfChar = ' ';
char nextChar = ' ';
bool flag = 0;
Node* next = NULL;
/* data */
};
vector<Node*> state;
Node* startNode = new Node();
Node* pNode = startNode;
state.push_back(pNode);
for(int i = 0; i < p.size(); i++)
{
if(i < p.size() - 1 && p[i+1] == '*')
{
pNode->selfChar = p[i];
pNode->nextChar = ' ';
Node* temp = new Node();
pNode->next = temp;
pNode = pNode->next;
state.push_back(pNode);
i += 1;
}
else
{
Node* temp = new Node();
pNode->next = temp;
pNode->nextChar = p[i];
pNode = pNode->next;
state.push_back(pNode);
}
}
vector<vector<int>> first;
for(int i = state.size()-1; i >= 0 ; i--)
{
vector<int> temp;
if (state[i]->nextChar != ' ' || state[i]->next == NULL){
temp.push_back(i);
first.push_back(temp);
}else{
temp = first[first.size()-1];
temp.push_back(i);
first.push_back(temp);
}
}
int len = first.size();
reverse(first.begin(), first.end());
set<int> match;
for(int i = 0; i < first[0].size(); i++){
match.insert(first[0][i]);
}
for(int i = 0; i < s.size(); i++)
{
vector<int> temp;
set<int>::iterator iter;
for(iter = match.begin(); iter != match.end(); iter++){
if(state[*iter]->nextChar == s[i] || state[*iter]->nextChar == '.'){
temp.insert(temp.end(), first[*iter+1].begin(), first[*iter+1].end());
}else if(state[*iter]->selfChar == s[i] || state[*iter]->selfChar == '.'){
temp.insert(temp.end(), first[*iter].begin(), first[*iter].end());
}
}
match.clear();
for(int j = 0; j < temp.size(); j++){
match.insert(temp[j]);
}
}
if (match.empty()) {
return false;
}else
{
if(match.find(len-1) != match.end()){
return true;
}else{
return false;
}
}
return 0;
}
};