Skip to content

Commit

Permalink
[DP] Refactor solution to Decode Ways
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Jan 13, 2020
1 parent aaced5c commit cb5974d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
29 changes: 10 additions & 19 deletions DP/DecodeWays.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,28 @@

class DecodeWays {
func numDecodings(_ s: String) -> Int {
let sChars = Array(s)
let s = Array(s)
var dp = Array(repeating: 0, count: s.count + 1)
dp[0] = 1

guard s.count >= 1 else {
return 0
}

for i in 1...s.count {
if String(sChars[i - 1..<i]).isValid {
if s[i - 1] != "0" {
dp[i] += dp[i - 1]
}
if i >= 2 && String(sChars[i - 2..<i]).isValid {

if i > 1 && isValid(s, i - 2, i - 1) {
dp[i] += dp[i - 2]
}
}
}

return dp[s.count]
}
}

extension String {
var isValid: Bool {
if let first = first, first == "0" {
return false
}

guard let num = Int(self) else {
return false

private func isValid(_ s: [Character], _ start: Int, _ end: Int) -> Bool {
guard let num = Int(String(s[start...end])) else {
fatalError()
}

return 0 < num && 26 >= num
return num >= 10 && num <= 26
}
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
![Leetcode](./logo.png?style=centerme)

## Progress
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 316 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 318 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.

## Contributors

Expand Down Expand Up @@ -226,7 +226,7 @@
[Nested List Weight Sum II](https://leetcode.com/problems/nested-list-weight-sum-ii/)| [Swift](./DP/NestedListWeightSumII.swift)| Medium| O(n)| O(n)|
[Flip Game II](https://leetcode.com/problems/flip-game-ii/)| [Swift](./DP/FlipGameII.swift)| Medium| O(n)| O(n)|
[Can I Win](https://leetcode.com/problems/can-i-win/)| [Swift](./DP/CanIWin.swift)| Medium| O(2^n)| O(n)|
[Decode Ways](https://leetcode.com/problems/decode-ways/)| [Swift](./DP/DecodeWays.swift) | O(n)|O(n)|
[Decode Ways](https://leetcode.com/problems/decode-ways/)| [Swift](./DP/DecodeWays.swift) | Medium| O(n)|O(n)|
[Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/)| [Swift](./DP/MinimumPathSum.swift)| Medium| O(mn)| O(mn)|
[Generate Parentheses](https://leetcode.com/problems/generate-parentheses/)| [Swift](./DP/GenerateParentheses.swift)| Medium| O(2^n)| O(n)|
[Different Ways to Add Parentheses](https://leetcode.com/problems/different-ways-to-add-parentheses/)| [Swift](./DP/DifferentWaysAddParentheses.swift)| Medium| O(n^n)| O(n)|
Expand Down

0 comments on commit cb5974d

Please sign in to comment.