Skip to content

Commit

Permalink
[DP] Update solution to Decode Ways
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Mar 31, 2018
1 parent 6db92c1 commit 7e9ae22
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions DP/DecodeWays.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,37 @@

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

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

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

return dp[len]
return dp[s.count]
}

private func isValid(_ numStr: String) -> Bool {
if Array(numStr.characters).first == "0" {
}

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

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

return num >= 1 && num <= 26
return 0 < num && 26 >= num
}
}

0 comments on commit 7e9ae22

Please sign in to comment.