LeetCode Online Judge 题目C# 练习 - Regular Expression Matching
2012-10-10 23:54
471 查看
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
代码分析:
递归。
1. 当s.Length == 0 的时候,看p是不是每个字符后面都有 '*',像 "a*.*j*u*",不然就返回false
2. 如果p递归到最后一个了,就只看if (p1 == '.' || p1 = s1), 所以 p2 随便给个 '0' 只要不是'*'都可以。
3. 1) 如果p2 == '*' 而且 p1 == '.' || s1 == p1, 那么就 递归进去 返回 p1 字符当 1 个用 或者 p1 字符当 0 个用 的结果。
2) 如果p2 != '*' 而且 p1 == '.' || s1 == p1, 那么递归进去 返回 p1之后的 与 s1之后的字符串 比较结果。 否者 返回 false
DP应该也是可以的,但是好像比较复杂。
'.' 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
public static bool RegularExpressionMatching(string s, string p) { //if s.Length == 0 check if p has * after each character if (s.Length == 0) return RegularExpressionMatching_CheckEmpty(p); //if s.Length >= 0 and p.Length == 0, return false if (p.Length == 0) return false; char s1 = s[0]; char p1 = p[0]; char p2 = p.Length > 1 ? p[1] : '0'; //any character but '*' will work if (p2 == '*') { if (p1 == '.' || s1 == p1) { return RegularExpressionMatching(s.Substring(1), p) || RegularExpressionMatching(s, p.Substring(2)); } else { return RegularExpressionMatching(s, p.Substring(2)); } } else { if (p1 == '.' || s1 == p1) { return RegularExpressionMatching(s.Substring(1), p.Substring(1)); } else return false; } } public static bool RegularExpressionMatching_CheckEmpty(string p) { if (p.Length % 2 != 0) return false; for (int i = 1; i < p.Length; i += 2) { if (p[i] != '*') return false; } return true; }
代码分析:
递归。
1. 当s.Length == 0 的时候,看p是不是每个字符后面都有 '*',像 "a*.*j*u*",不然就返回false
2. 如果p递归到最后一个了,就只看if (p1 == '.' || p1 = s1), 所以 p2 随便给个 '0' 只要不是'*'都可以。
3. 1) 如果p2 == '*' 而且 p1 == '.' || s1 == p1, 那么就 递归进去 返回 p1 字符当 1 个用 或者 p1 字符当 0 个用 的结果。
2) 如果p2 != '*' 而且 p1 == '.' || s1 == p1, 那么递归进去 返回 p1之后的 与 s1之后的字符串 比较结果。 否者 返回 false
DP应该也是可以的,但是好像比较复杂。
相关文章推荐
- LeetCode Online Judge 题目C# 练习 - 3SUM Closest
- LeetCode Online Judge 题目C# 练习 - Search Insert Position
- LeetCode Online Judge 题目C# 练习 - Set Matrix Zeroes
- LeetCode Online Judge 题目C# 练习 - Two Sum
- LeetCode Online Judge 题目C# 练习 - Binary Tree Level Order Traversal II
- LeetCode Online Judge 题目C# 练习 - N-Queens
- LeetCode Online Judge 题目C# 练习 - Minimum Window Substring
- LeetCode Online Judge 题目C# 练习 - Same Tree
- LeetCode Online Judge 题目C# 练习 - Edit Distance
- LeetCode Online Judge 题目C# 练习 - String to Integer (atoi)
- LeetCode Online Judge 题目C# 练习 - Unique Binary Search Trees II
- LeetCode Online Judge 题目C# 练习 - Reverse Nodes in k-Group
- LeetCode Online Judge 题目C# 练习 - Sprial Matrix
- LeetCode Online Judge 题目C# 练习 - Unique Paths
- LeetCode Online Judge 题目C# 练习 - Climbing Stairs
- LeetCode Online Judge 题目C# 练习 - Maximal Rectangle
- LeetCode Online Judge 题目C# 练习 - Median of Two Sorted Arrays
- LeetCode Online Judge 题目C# 练习 - Merge k Sorted Lists
- LeetCode Online Judge 题目C# 练习 - Anagrams
- LeetCode Online Judge 题目C# 练习 - Maximum Subarray