File tree 1 file changed +39
-0
lines changed
1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ package hard ;
2
+
3
+ /**
4
+ * Created by fishercoder1534 on 10/3/16.
5
+ */
6
+ public class RegularExpressionMatching {
7
+
8
+ public boolean isMatch (String s , String p ) {
9
+ if (s == null || p == null ) {
10
+ return false ;
11
+ }
12
+ boolean [][] dp = new boolean [s .length ()+1 ][p .length ()+1 ];
13
+ dp [0 ][0 ] = true ;
14
+ for (int i = 0 ; i < p .length (); i ++) {
15
+ if (p .charAt (i ) == '*' && dp [0 ][i -1 ]) {
16
+ dp [0 ][i +1 ] = true ;
17
+ }
18
+ }
19
+ for (int i = 0 ; i < s .length (); i ++) {
20
+ for (int j = 0 ; j < p .length (); j ++) {
21
+ if (p .charAt (j ) == '.' ) {
22
+ dp [i +1 ][j +1 ] = dp [i ][j ];
23
+ }
24
+ if (p .charAt (j ) == s .charAt (i )) {
25
+ dp [i +1 ][j +1 ] = dp [i ][j ];
26
+ }
27
+ if (p .charAt (j ) == '*' ) {
28
+ if (p .charAt (j -1 ) != s .charAt (i ) && p .charAt (j -1 ) != '.' ) {
29
+ dp [i +1 ][j +1 ] = dp [i +1 ][j -1 ];
30
+ } else {
31
+ dp [i +1 ][j +1 ] = (dp [i +1 ][j ] || dp [i ][j +1 ] || dp [i +1 ][j -1 ]);
32
+ }
33
+ }
34
+ }
35
+ }
36
+ return dp [s .length ()][p .length ()];
37
+ }
38
+
39
+ }
You can’t perform that action at this time.
0 commit comments