forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtedkimdev.rs
More file actions
34 lines (31 loc) · 872 Bytes
/
tedkimdev.rs
File metadata and controls
34 lines (31 loc) · 872 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// SC: O(t/m) -> maximum recursion depth
// TC: O(2^(t/m))
// t = target value
// m = smallest number in nums
// t/m = maximum number of elements in a combination
impl Solution {
pub fn num_decodings(s: String) -> i32 {
if s.starts_with('0') {
return 0;
}
// dfs
fn dfs(s: &str) -> i32 {
if s.is_empty() {
return 1;
}
let mut output = 0;
if s[..1].parse::<i32>().unwrap() != 0 {
output += dfs(&s[1..]);
}
if s.len() >= 2 && !s[..2].starts_with('0') {
if s[..2].parse::<i32>().unwrap() > 26 {
return output;
} else {
output += dfs(&s[2..]);
}
}
output
}
dfs(&s)
}
}