Skip to content

Commit

Permalink
feat: Add solution for LeetCode problem 91
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiteHyun committed Jul 13, 2024
1 parent 972d98e commit 2f0e23e
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions decode-ways/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// 91. Decode Ways
// https://leetcode.com/problems/decode-ways/description/
// Dale-Study
//
// Created by WhiteHyun on 2024/07/14.
//

class Solution {

// dp
func numDecodings(_ s: String) -> Int {
var (current, previous) = (1, 0)
let array = s.compactMap { Int(String($0)) }
for index in array.indices.reversed() {
if array[index] == 0 {
(current, previous) = (0, current)
} else if index + 1 < array.count, array[index] * 10 + array[index + 1] <= 26 {
(current, previous) = (current + previous, current)
} else {
previous = current
}
}

return current
}

// Memoization
func numDecodingsUsingMemoization(_ s: String) -> Int {
let messages = s.compactMap { Int(String($0)) }
var cache: [Int: Int] = [s.count: 1]

func dfs(_ index: Int) -> Int {
if let cached = cache[index] {
return cached
}

if messages[index] == 0 {
cache[index] = 0
} else if index + 1 < s.count && messages[index] * 10 + messages[index + 1] < 27 {
cache[index] = dfs(index + 1) + dfs(index + 2)
} else {
cache[index] = dfs(index + 1)
}
return cache[index]!
}

return dfs(0)
}
}

0 comments on commit 2f0e23e

Please sign in to comment.