Skip to content

Commit

Permalink
[DP] Add a solution to Regular Expression Matching
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Apr 17, 2018
1 parent 04832f7 commit c2669ac
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
36 changes: 36 additions & 0 deletions DP/RegularExpressionMatching.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Question Link: https://leetcode.com/problems/regular-expression-matching/
* Primary idea: Classic Two Dimensionel Dynamic Programming
* Time Complexity: O(mn), Space Complexity: O(mn)
*/

class RegularExpressionMatching {
func isMatch(_ s: String, _ p: String) -> Bool {
let sChars = Array(s), pChars = Array(p)
var dp = Array(repeating: Array(repeating: false, count: pChars.count + 1), count: sChars.count + 1)
dp[0][0] = true

for i in 0...pChars.count {
// jump over "" vs. "x*" case
dp[0][i] = i == 0 || i > 1 && dp[0][i - 2] && pChars[i - 1] == "*"
}

for i in 0...sChars.count {
for j in 0...pChars.count {
guard j > 0 else {
continue
}

let pCurrent = pChars[j - 1]

if pCurrent != "*" {
dp[i][j] = i > 0 && dp[i - 1][j - 1] && (pCurrent == "." || pCurrent == sChars[i - 1])
} else {
dp[i][j] = dp[i][j - 2] || i > 0 && j > 1 && (sChars[i - 1] == pChars[j - 2] || pChars[j - 2] == ".") && dp[i - 1][j]
}
}
}

return dp[sChars.count][pChars.count]
}
}
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* [Microsoft](#microsoft)

## Progress
[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).
[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).


## Array
Expand Down Expand Up @@ -194,6 +194,7 @@
[Combination Sum IV](https://leetcode.com/problems/combination-sum-iv/)| [Swift](./DP/CombinationSumIV.swift)| Medium| O(2^n)| O(n)|
[Triangle](https://leetcode.com/problems/triangle/)| [Swift](./DP/Triangle.swift)| Medium| O(2^n - 1)| O(m)|
[Wildcard Matching](https://leetcode.com/problems/wildcard-matching/)| [Swift](./DP/WildcardMatching.swift)| Hard| O(mn)| O(mn)|
[Regular Expression Matching](https://leetcode.com/problems/regular-expression-matching/)| [Swift](./DP/RegularExpressionMatching.swift)| Hard| O(mn)| O(mn)|
[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)|
[Burst Ballons](https://leetcode.com/problems/burst-balloons/)| [Swift](./DP/BurstBalloons.swift)| Hard| O(n^3)| O(n)|
[Frog Jump](https://leetcode.com/problems/frog-jump/)| [Swift](./DP/FrogJump.swift)| Hard| O(n^2)| O(n)|
Expand Down Expand Up @@ -741,7 +742,7 @@
| [Swift](./DFS/PermutationsII.swift) | 47 | [Permutations II](https://oj.leetcode.com/problems/permutations-ii/) | Medium |
| [Swift](./DFS/Permutations.swift) | 46 | [Permutations](https://oj.leetcode.com/problems/permutations/) | Medium |
| | 45 | [Jump Game II](https://oj.leetcode.com/problems/jump-game-ii/) | Hard |
| | 44 | [Wildcard Matching](https://oj.leetcode.com/problems/wildcard-matching/) | Hard |
| [Swift](./DP/WildcardMatching.swift) | 44 | [Wildcard Matching](https://oj.leetcode.com/problems/wildcard-matching/) | Hard |
| [Swift](./String/MultiplyStrings.swift) | 43 | [Multiply Strings](https://oj.leetcode.com/problems/multiply-strings/) | Medium |
| [Swift](./Math/TrappingRainWater.swift) | 42 | [Trapping Rain Water](https://oj.leetcode.com/problems/trapping-rain-water/) | Hard |
| | 41 | [First Missing Positive](https://oj.leetcode.com/problems/first-missing-positive/) | Hard |
Expand Down Expand Up @@ -775,7 +776,7 @@
| [Swift](./Math/RomanToInteger.swift) | 13 | [Roman to Integer](https://oj.leetcode.com/problems/roman-to-integer/) | Easy |
| [Swift](./Math/IntegerToRoman.swift) | 12 | [Integer to Roman](https://oj.leetcode.com/problems/integer-to-roman/) | Medium |
| [Swift](./Math/ContainerMostWater.swift) | 11 | [Container With Most Water](https://oj.leetcode.com/problems/container-with-most-water/) | Medium |
| | 10 | [Regular Expression Matching](https://oj.leetcode.com/problems/regular-expression-matching/) | Hard |
| [Swift](./DP/RegularExpressionMatching.swift) | 10 | [Regular Expression Matching](https://oj.leetcode.com/problems/regular-expression-matching/) | Hard |
| [Swift](./Math/PalindromeNumber.swift) | 9 | [Palindrome Number](https://oj.leetcode.com/problems/palindrome-number/) | Easy |
| [Swift](./Math/Atoi.swift) | 8 | [String to Integer (atoi)](https://oj.leetcode.com/problems/string-to-integer-atoi/) | Easy |
| [Swift](./Math/ReverseInteger.swift) | 7 | [Reverse Integer](https://oj.leetcode.com/problems/reverse-integer/) | Easy |
Expand Down

0 comments on commit c2669ac

Please sign in to comment.