|
| 1 | +packageAlgorithms.dp; |
| 2 | + |
| 3 | +publicclassIsMatch { |
| 4 | +publicstaticvoidmain(String[]strs) { |
| 5 | +// System.out.println(isMatch("aa","a")); |
| 6 | +// System.out.println(isMatch("aa","aa**")); |
| 7 | +// System.out.println(isMatch("aaa","aa")); |
| 8 | +System.out.println(isMatch("hi","*?")); |
| 9 | +// System.out.println(isMatch("aa","a*")); |
| 10 | +// System.out.println(isMatch("ab","?*")); |
| 11 | +// System.out.println(isMatch("aab", "c*a*b")); |
| 12 | + } |
| 13 | + |
| 14 | +publicstaticbooleanisMatch1(Strings,Stringp) { |
| 15 | +if (s ==null ||p ==null) { |
| 16 | +returnfalse; |
| 17 | + } |
| 18 | + |
| 19 | +intlens =s.length(); |
| 20 | +intlenp =p.length(); |
| 21 | + |
| 22 | +// 创建一个Dp二维数组 |
| 23 | +boolean[][]D =newboolean[lens +1][lenp +1]; |
| 24 | + |
| 25 | +booleanflag =false; |
| 26 | + |
| 27 | +for (inti =0;i <=lens;i++) { |
| 28 | +flag =false; |
| 29 | +for (intj =0;j <=lenp;j++) { |
| 30 | +// both is empty. |
| 31 | +if (i ==0 &&j ==0) { |
| 32 | +D[i][j] =true; |
| 33 | +flag =true; |
| 34 | +continue; |
| 35 | + } |
| 36 | + |
| 37 | +// if P is empty, s is not empty, it is false. |
| 38 | +if (j ==0) { |
| 39 | +D[i][j] =false; |
| 40 | +continue; |
| 41 | + } |
| 42 | + |
| 43 | +// if S is empty, P is not empty |
| 44 | +if (i ==0) { |
| 45 | +D[i][j] =D[i][j -1] &&p.charAt(j -1) =='*'; |
| 46 | + }else { |
| 47 | +D[i][j] = (matchChar(s.charAt(i -1),p.charAt(j -1)) &&D[i -1][j -1]) |
| 48 | + || (p.charAt(j -1) =='*' && (D[i][j -1] ||D[i -1][j])); |
| 49 | + } |
| 50 | + |
| 51 | +if (D[i][j]) { |
| 52 | +flag =true; |
| 53 | + } |
| 54 | + |
| 55 | +// Greedy. 在此即可以退出,因为* 可以匹配余下的所有的字符串。 |
| 56 | +if (D[i][j] &&p.charAt(j -1) =='*' &&j ==lenp) { |
| 57 | +returntrue; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | +if (!flag) { |
| 62 | +returnfalse; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | +returnD[lens][lenp]; |
| 67 | + } |
| 68 | + |
| 69 | +publicstaticbooleanmatchChar(charc,charp) { |
| 70 | +return (p =='?' ||p ==c); |
| 71 | + } |
| 72 | + |
| 73 | +publicstaticbooleanisMatch(Strings,Stringp) { |
| 74 | +if (s ==null ||p ==null) { |
| 75 | +returnfalse; |
| 76 | + } |
| 77 | + |
| 78 | +intindexS =0; |
| 79 | +intindexP =0; |
| 80 | + |
| 81 | +intlenS =s.length(); |
| 82 | +intlenP =p.length(); |
| 83 | + |
| 84 | +intpreS =0; |
| 85 | +intpreP =0; |
| 86 | + |
| 87 | +booleanback =false; |
| 88 | + |
| 89 | +while (indexS <lenS) { |
| 90 | +if (indexP <lenP &&matchChar(s.charAt(indexS),p.charAt(indexP))) { |
| 91 | +indexS++; |
| 92 | +indexP++; |
| 93 | + }elseif (indexP <lenP &&p.charAt(indexP) =='*') { |
| 94 | +while (indexP <lenP &&p.charAt(indexP) =='*') { |
| 95 | +indexP++; |
| 96 | + } |
| 97 | + |
| 98 | +//P的最后一个是 *,表示可以匹配任何字符串 |
| 99 | +if (indexP ==lenP) { |
| 100 | +returntrue; |
| 101 | + } |
| 102 | + |
| 103 | +// 记录下这个匹配位置。 |
| 104 | +back =true; |
| 105 | +preS =indexS; |
| 106 | +preP =indexP; |
| 107 | + }else { |
| 108 | +if (back) { |
| 109 | +indexS = ++preS; |
| 110 | +indexP =preP; |
| 111 | + }else { |
| 112 | +returnfalse; |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | +if (indexS ==lenS &&indexP ==lenP) { |
| 118 | +returntrue; |
| 119 | + } |
| 120 | + |
| 121 | +returnfalse; |
| 122 | + } |
| 123 | +} |