Skip to content

Commit

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

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

// must start from 0, to make range feasible and handle empty vs. * case
for i in 0...s.count {
for j in 0...p.count {
guard j > 0 else {
continue
}

let pCurrent = pChars[j - 1]

if pCurrent != "*" {
dp[i][j] = i > 0 && dp[i - 1][j - 1] && (pCurrent == sChars[i - 1] || pCurrent == "?")
} else {
// Two situations:
// (1) '*' is the first character in p;
// (2) For k>=0 and k<=i, there is some dp[k][j-1] being true;
// and '*' will match the rest sequence in s after index k;
var flag = false
for k in 0...i {
if dp[k][j - 1] {
flag = true
break
}
}

dp[i][j] = flag || j == 1
}
}
}

return dp[s.count][p.count]
}
}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@
* [Microsoft](#microsoft)

## Progress
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 232 completed solutions. Note: questions with &hearts; 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 233 completed solutions. Note: questions with &hearts; 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
@@ -193,6 +193,7 @@
[Edit Distance](https://leetcode.com/problems/edit-distance/)| [Swift](./DP/EditDistance.swift)| Hard| O(mn)| O(mn)|
[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)|
[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)|

0 comments on commit 04832f7

Please sign in to comment.