Skip to content

Commit 0f32751

Browse files
committed
regular_expression_matching
1 parent 0ba6c17 commit 0f32751

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
1111
#### [7. reverse integer](https://.com/hitzzc/go-leetcode/tree/master/reverse_integer)
1212
#### [8. string to integer](https://.com/hitzzc/go-leetcode/tree/master/string_to_integer)
1313
#### [9. palindrome number](https://.com/hitzzc/go-leetcode/tree/master/palindrome_number)
14+
#### [10. Regular Expression Matching](https://.com/hitzzc/go-leetcode/tree/master/regular_expression_matching)
1415
#### [11. container with most water](https://.com/hitzzc/go-leetcode/tree/master/container_with_most_water)
1516
#### [12. integer to roman](https://.com/hitzzc/go-leetcode/tree/master/integer_to_roman)
1617
#### [13. roman to integer](https://.com/hitzzc/go-leetcode/tree/master/roman_to_integer)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
bool isMatch(string s, string p) {
4+
return isMatch(s, p, s.size(), p.size());
5+
}
6+
bool isMatch(string& s, string& p, int i, int j) {
7+
if (i == 0 && j == 0) return true;
8+
if (i != 0 && j == 0) return false;
9+
if (i == 0 && j != 0) {
10+
if (p[j-1] == '*') {
11+
return isMatch(s, p, i, j-2);
12+
}
13+
return false;
14+
}
15+
if (s[i-1] == p[j-1] || p[j-1] == '.') {
16+
return isMatch(s, p, i-1, j-1);
17+
} else if (p[j-1] == '*') {
18+
if (isMatch(s, p, i, j-2)) return true;
19+
if (s[i-1] == p[j-2] || p[j-2] == '.') {
20+
return isMatch(s, p, i-1, j);
21+
}
22+
return false;
23+
}
24+
return false;
25+
}
26+
};

0 commit comments

Comments
 (0)