File tree 1 file changed +8
-7
lines changed
scripts/algorithms/R/Regular Expression Matching
1 file changed +8
-7
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 5 ms (Top 57.68%) | Memory: 42.9 MB (Top 39.28%)
1
2
class Solution {
2
3
public boolean isMatch (String s , String p ) {
3
4
if (p == null || p .length () == 0 ) return (s == null || s .length () == 0 );
4
-
5
+
5
6
boolean dp [][] = new boolean [s .length ()+1 ][p .length ()+1 ];
6
7
dp [0 ][0 ] = true ;
7
8
for (int j =2 ; j <=p .length (); j ++) {
8
- dp [0 ][j ] = p .charAt (j -1 ) == '*' && dp [0 ][j -2 ];
9
+ dp [0 ][j ] = p .charAt (j -1 ) == '*' && dp [0 ][j -2 ];
9
10
}
10
-
11
+
11
12
for (int j =1 ; j <=p .length (); j ++) {
12
13
for (int i =1 ; i <=s .length (); i ++) {
13
- if (p .charAt (j -1 ) == s .charAt (i -1 ) || p .charAt (j -1 ) == '.' )
14
- dp [i ][j ] = dp [i -1 ][j -1 ];
14
+ if (p .charAt (j -1 ) == s .charAt (i -1 ) || p .charAt (j -1 ) == '.' )
15
+ dp [i ][j ] = dp [i -1 ][j -1 ];
15
16
else if (p .charAt (j -1 ) == '*' )
16
- dp [i ][j ] = dp [i ][j -2 ] || ((s .charAt (i -1 ) == p .charAt (j -2 ) || p .charAt (j -2 ) == '.' ) && dp [i -1 ][j ]);
17
+ dp [i ][j ] = dp [i ][j -2 ] || ((s .charAt (i -1 ) == p .charAt (j -2 ) || p .charAt (j -2 ) == '.' ) && dp [i -1 ][j ]);
17
18
}
18
19
}
19
20
return dp [s .length ()][p .length ()];
20
21
}
21
- }
22
+ }
You can’t perform that action at this time.
0 commit comments