判断正则表达式是否匹配 Regular Expression Matching @LeetCode

前端之家收集整理的这篇文章主要介绍了判断正则表达式是否匹配 Regular Expression Matching @LeetCode前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

这道题真心容易错,总的来说思路是递归。

判断下一个字符是否是*:

如果不是*,则判断当前字符是否匹配。

如果是*,则因为不能确定*到底会匹配几个,在当前字符匹配的前提下,要枚举所有的情况,从假设匹配0个,1个,2个。。。只要有一种情况成功了,最终也就成功了。

如果是*,但是当前字符不匹配,则跳过两个递归。


http://discuss.leetcode.com/questions/175/regular-expression-matching

这里有更详细的解释。



  1. package Level5;
  2.  
  3.  
  4. /**
  5. Regular Expression Matching
  6.  
  7. Implement regular expression matching with support for '.' and '*'.
  8.  
  9. '.' Matches any single character.
  10. '*' Matches zero or more of the preceding element.
  11.  
  12. The matching should cover the entire input string (not partial).
  13.  
  14. The function prototype should be:
  15. bool isMatch(const char *s,const char *p)
  16.  
  17. Some examples:
  18. isMatch("aa","a") → false
  19. isMatch("aa","aa") → true
  20. isMatch("aaa","aa") → false
  21. isMatch("aa","a*") → true
  22. isMatch("aa",".*") → true
  23. isMatch("ab",".*") → true
  24. isMatch("aab","c*a*b") → true
  25. Discuss
  26.  
  27.  
  28. */
  29. public class S10 {
  30.  
  31. public static void main(String[] args) {
  32. System.out.println(isMatch("aa","a*")); // true
  33. System.out.println(isMatch("aa","a")); // false
  34. System.out.println(isMatch("aa","aa")); // true
  35. System.out.println(isMatch("aaa","aa")); // false
  36. System.out.println(isMatch("aa","a*")); //true
  37. System.out.println(isMatch("aa",".*")); // true
  38. System.out.println(isMatch("ab",".*")); // true
  39. System.out.println(isMatch("aab","c*a*b")); // true
  40. System.out.println(isMatch("","bab")); // false
  41. System.out.println(isMatch("ab",".*c")); // false
  42. System.out.println(isMatch("aaa","a.a")); // true
  43. System.out.println(isMatch("a",".")); // true
  44. System.out.println(isMatch("a","ab*")); // true
  45. System.out.println(isMatch("bb",".bab")); // false
  46. System.out.println(isMatch("bb","*")); // false
  47. }
  48.  
  49. public static boolean isMatch(String s,String p) {
  50. return isM(s,p,0);
  51. }
  52. public static boolean isM(String s,String p,int i,int j){
  53. if(j >= p.length()){ // pattern已经用光
  54. return i >= s.length(); // 如果s已经走完则匹配,否则不匹配
  55. }
  56. if(j == p.length()-1){ // 如果pattern的j已经走到最后一个字符,如果想匹配,则s的i也必须在最后一个,且相等
  57. return (i == s.length()-1) && (s.charAt(i)==p.charAt(j) || p.charAt(j)=='.');
  58. }
  59.  
  60. // 如果pattern的下一个字符(j+1)不是*
  61. if(j+1<p.length() && p.charAt(j+1) != '*'){
  62. if(i == s.length()){ // 如果s已经走完,则说明不匹配
  63. return false;
  64. }
  65. if(s.charAt(i)==p.charAt(j) || p.charAt(j)=='.'){ // 如果当前字符匹配
  66. return isM(s,i+1,j+1); // 继续下一个字符判断
  67. }else{ // 如果当前字符不匹配,直接返回false
  68. return false;
  69. }
  70. }
  71. // 如果下一个字符(j+1)是* 且 当前字符匹配,则进行搜索
  72. while(i<s.length() && j<p.length() && (s.charAt(i)==p.charAt(j) || p.charAt(j)=='.')){
  73. // 因为*可以取0,1,2,...所以i=i,i+2,...对所有可能进行测试
  74. // 最后能否匹配取决于剩下的匹配是否成功
  75. if(isM(s,i,j+2)){ // 只要找到一个匹配成功即可
  76. return true;
  77. }
  78. i++;
  79. }
  80. // 如果下一个字符(j+1)是* 且 当前字符不匹配,则pattern跳过两个,继续比较
  81. // 还有一种情况到这里是上面的最后一次尝试(i==s.length())
  82. return isM(s,j+2);
  83. }
  84.  
  85. }

猜你在找的正则表达式相关文章