Skip to content

Commit cb5974d

Browse files
committed
[DP] Refactor solution to Decode Ways
1 parent aaced5c commit cb5974d

File tree

2 files changed

+12
-21
lines changed

2 files changed

+12
-21
lines changed

DP/DecodeWays.swift

+10-19
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,28 @@
88

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

15-
guard s.count >= 1 else {
16-
return 0
17-
}
18-
1915
for i in 1...s.count {
20-
if String(sChars[i - 1..<i]).isValid {
16+
if s[i - 1] != "0" {
2117
dp[i] += dp[i - 1]
2218
}
23-
if i >= 2 && String(sChars[i - 2..<i]).isValid {
19+
20+
if i > 1 && isValid(s, i - 2, i - 1) {
2421
dp[i] += dp[i - 2]
25-
}
22+
}
2623
}
2724

2825
return dp[s.count]
2926
}
30-
}
31-
32-
extension String {
33-
var isValid: Bool {
34-
if let first = first, first == "0" {
35-
return false
36-
}
37-
38-
guard let num = Int(self) else {
39-
return false
27+
28+
private func isValid(_ s: [Character], _ start: Int, _ end: Int) -> Bool {
29+
guard let num = Int(String(s[start...end])) else {
30+
fatalError()
4031
}
4132

42-
return 0 < num && 26 >= num
33+
return num >= 10 && num <= 26
4334
}
4435
}

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
![Leetcode](./logo.png?style=centerme)
55

66
## Progress
7-
[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.
7+
[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.
88

99
## Contributors
1010

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

0 commit comments

Comments
 (0)