forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add solution for LeetCode problem 91
- Loading branch information
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |