Skip to content

Commit c2669ac

Browse files
committed
[DP] Add a solution to Regular Expression Matching
1 parent 04832f7 commit c2669ac

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

DP/RegularExpressionMatching.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/regular-expression-matching/
3+
* Primary idea: Classic Two Dimensionel Dynamic Programming
4+
* Time Complexity: O(mn), Space Complexity: O(mn)
5+
*/
6+
7+
class RegularExpressionMatching {
8+
func isMatch(_ s: String, _ p: String) -> Bool {
9+
let sChars = Array(s), pChars = Array(p)
10+
var dp = Array(repeating: Array(repeating: false, count: pChars.count + 1), count: sChars.count + 1)
11+
dp[0][0] = true
12+
13+
for i in 0...pChars.count {
14+
// jump over "" vs. "x*" case
15+
dp[0][i] = i == 0 || i > 1 && dp[0][i - 2] && pChars[i - 1] == "*"
16+
}
17+
18+
for i in 0...sChars.count {
19+
for j in 0...pChars.count {
20+
guard j > 0 else {
21+
continue
22+
}
23+
24+
let pCurrent = pChars[j - 1]
25+
26+
if pCurrent != "*" {
27+
dp[i][j] = i > 0 && dp[i - 1][j - 1] && (pCurrent == "." || pCurrent == sChars[i - 1])
28+
} else {
29+
dp[i][j] = dp[i][j - 2] || i > 0 && j > 1 && (sChars[i - 1] == pChars[j - 2] || pChars[j - 2] == ".") && dp[i - 1][j]
30+
}
31+
}
32+
}
33+
34+
return dp[sChars.count][pChars.count]
35+
}
36+
}

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* [Microsoft](#microsoft)
2828

2929
## Progress
30-
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 233 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
30+
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 234 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
3131

3232

3333
## Array
@@ -194,6 +194,7 @@
194194
[Combination Sum IV](https://leetcode.com/problems/combination-sum-iv/)| [Swift](./DP/CombinationSumIV.swift)| Medium| O(2^n)| O(n)|
195195
[Triangle](https://leetcode.com/problems/triangle/)| [Swift](./DP/Triangle.swift)| Medium| O(2^n - 1)| O(m)|
196196
[Wildcard Matching](https://leetcode.com/problems/wildcard-matching/)| [Swift](./DP/WildcardMatching.swift)| Hard| O(mn)| O(mn)|
197+
[Regular Expression Matching](https://leetcode.com/problems/regular-expression-matching/)| [Swift](./DP/RegularExpressionMatching.swift)| Hard| O(mn)| O(mn)|
197198
[Guess Number Higher or Lower II](https://leetcode.com/problems/guess-number-higher-or-lower-ii/)| [Swift](./DP/GuessNumberHigherOrLowerII.swift)| Medium| O(nlogn)| O(n^2)|
198199
[Burst Ballons](https://leetcode.com/problems/burst-balloons/)| [Swift](./DP/BurstBalloons.swift)| Hard| O(n^3)| O(n)|
199200
[Frog Jump](https://leetcode.com/problems/frog-jump/)| [Swift](./DP/FrogJump.swift)| Hard| O(n^2)| O(n)|
@@ -741,7 +742,7 @@
741742
| [Swift](./DFS/PermutationsII.swift) | 47 | [Permutations II](https://oj.leetcode.com/problems/permutations-ii/) | Medium |
742743
| [Swift](./DFS/Permutations.swift) | 46 | [Permutations](https://oj.leetcode.com/problems/permutations/) | Medium |
743744
| | 45 | [Jump Game II](https://oj.leetcode.com/problems/jump-game-ii/) | Hard |
744-
| | 44 | [Wildcard Matching](https://oj.leetcode.com/problems/wildcard-matching/) | Hard |
745+
| [Swift](./DP/WildcardMatching.swift) | 44 | [Wildcard Matching](https://oj.leetcode.com/problems/wildcard-matching/) | Hard |
745746
| [Swift](./String/MultiplyStrings.swift) | 43 | [Multiply Strings](https://oj.leetcode.com/problems/multiply-strings/) | Medium |
746747
| [Swift](./Math/TrappingRainWater.swift) | 42 | [Trapping Rain Water](https://oj.leetcode.com/problems/trapping-rain-water/) | Hard |
747748
| | 41 | [First Missing Positive](https://oj.leetcode.com/problems/first-missing-positive/) | Hard |
@@ -775,7 +776,7 @@
775776
| [Swift](./Math/RomanToInteger.swift) | 13 | [Roman to Integer](https://oj.leetcode.com/problems/roman-to-integer/) | Easy |
776777
| [Swift](./Math/IntegerToRoman.swift) | 12 | [Integer to Roman](https://oj.leetcode.com/problems/integer-to-roman/) | Medium |
777778
| [Swift](./Math/ContainerMostWater.swift) | 11 | [Container With Most Water](https://oj.leetcode.com/problems/container-with-most-water/) | Medium |
778-
| | 10 | [Regular Expression Matching](https://oj.leetcode.com/problems/regular-expression-matching/) | Hard |
779+
| [Swift](./DP/RegularExpressionMatching.swift) | 10 | [Regular Expression Matching](https://oj.leetcode.com/problems/regular-expression-matching/) | Hard |
779780
| [Swift](./Math/PalindromeNumber.swift) | 9 | [Palindrome Number](https://oj.leetcode.com/problems/palindrome-number/) | Easy |
780781
| [Swift](./Math/Atoi.swift) | 8 | [String to Integer (atoi)](https://oj.leetcode.com/problems/string-to-integer-atoi/) | Easy |
781782
| [Swift](./Math/ReverseInteger.swift) | 7 | [Reverse Integer](https://oj.leetcode.com/problems/reverse-integer/) | Easy |

0 commit comments

Comments
 (0)