LeetCode - Regular Expression Matching
2014-03-01 21:09
309 查看
Regular Expression Matching
2014.3.1 20:55
Implement regular expression matching with support for
Solution:
This problem has some connection with Wildcard Matching, but the backtracking strategy is a bit different.
In this problem, my solution is to match the letters and record the '*'s as they appear.
When a mismatch happened, you keep backtracking until a match is found, while in "Wildcard Matching", you may only backtrack to the last '*'.
If you have no where else to backtrack and there is not a single match for the current letter, return false.
Still, you have to ignore the redundant '*'s at the tail of the pattern string if there are any.
Total time complexity is O(len(s) * len(p)), but it wouldn't appear that large. I guess for more of the test cases it is near O(len(s) + len(p)). Space complexity is O(len(p)), as it is used to record the appearance of '*'s.
Accepted code:
2014.3.1 20:55
Implement regular expression matching with support for
'.'and
'*'.
'.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true
Solution:
This problem has some connection with Wildcard Matching, but the backtracking strategy is a bit different.
In this problem, my solution is to match the letters and record the '*'s as they appear.
When a mismatch happened, you keep backtracking until a match is found, while in "Wildcard Matching", you may only backtrack to the last '*'.
If you have no where else to backtrack and there is not a single match for the current letter, return false.
Still, you have to ignore the redundant '*'s at the tail of the pattern string if there are any.
Total time complexity is O(len(s) * len(p)), but it wouldn't appear that large. I guess for more of the test cases it is near O(len(s) + len(p)). Space complexity is O(len(p)), as it is used to record the appearance of '*'s.
Accepted code:
// 1RE, 4WA, 1AC, that was quite a problem... no DP and no recursion is tough. #include <cstring> #include <vector> using namespace std; class Solution { public: bool isMatch(const char *s, const char *p) { int i, j; int ls, lp; vector<int> last_i_arr; vector<int> last_j_arr; if (s == nullptr || p == nullptr) { return false; } ls = strlen(s); lp = strlen(p); if (lp == 0) { // empty patterns are regarded as match. return ls == 0; } // validate the pattern string. for (j = 0; j < lp; ++j) { if (p[j] == '*' && (j == 0 || p[j - 1] == '*')) { // invalid pattern string, can't match. return false; } } int last_i, last_j; i = j = 0; last_i = -1; last_j = -1; while (i < ls) { if (j + 1 < lp && p[j + 1] == '*') { last_i_arr.push_back(i); last_j_arr.push_back(j); ++last_i; ++last_j; j += 2; } else if (p[j] == '.' || s[i] == p[j]) { ++i; ++j; } else if (last_j != -1) { if (p[last_j_arr[last_j]] == '.' || s[last_i_arr[last_i]] == p[last_j_arr[last_j]]) { // current backtracking position is still available. i = (++last_i_arr[last_i]); j = last_j_arr[last_j] + 2; } else if (last_j > 0) { while (last_j >= 0) { // backtrack to the last backtracking point. --last_i; --last_j; last_i_arr.pop_back(); last_j_arr.pop_back(); if (last_j >= 0 && (p[last_j_arr[last_j]] == '.' || s[last_i_arr[last_i]] == p[last_j_arr[last_j]])) { i = (++last_i_arr[last_i]); j = last_j_arr[last_j] + 2; break; } } if (last_j == -1) { return false; } } else { // no more backtracking is possible. return false; } } else { return false; } } while (j < lp) { if (j + 1 < lp && p[j + 1] == '*') { j += 2; } else { break; } } last_i_arr.clear(); last_j_arr.clear(); return j == lp; } };
相关文章推荐
- HDU1896Stones(优先队列)
- OpenERP中自定义模块卸载失败,Postgres数据库删不掉数据库,OpenERP登录不了一直在加载的问题解决方案。
- UVA1513--Movie collection--线段树
- 字符串替换
- 默认情况下,使用IIS作为Web服务器的无法下载apk文件
- 深网的概念
- Oracle之监听的作用
- python图像包PIL学习
- C# 实现多线程的同步方法详解
- 假币的型号
- 双系统(windows8和Ubuntu12.04)启动时出现grub rescue
- 周期串
- yaffs2文件系统介绍 .
- linux网络流程分析(一)---网卡驱动
- yaffs2文件系统介绍
- 江南Style之---西湖
- GTD 实战与总结
- VC基本操作四十条
- HDU 4135容斥原理求互质的个数
- HDU 4745 Two Rabbits (最长回文变形)