-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1416-Restore_The_Array.rs
More file actions
35 lines (28 loc) · 901 Bytes
/
Copy path1416-Restore_The_Array.rs
File metadata and controls
35 lines (28 loc) · 901 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
35
impl Solution {
pub fn number_of_arrays(s: String, k: i32) -> i32 {
const MOD: i64 = 1_000_000_007;
let n = s.len();
let s_bytes = s.as_bytes();
let k = k as i64;
let mut dp = vec![0i64; n + 1];
dp[0] = 1;
for i in 1..=n {
for len in 1..=11.min(i) {
let j = i - len;
if s_bytes[j] == b'0' {
continue;
}
let substring = &s[j..i];
let num = match substring.parse::<i64>() {
Ok(n) => n,
Err(_) => break,
};
if num > k {
break;
}
dp[i] = (dp[i] + dp[j]) % MOD;
}
}
dp[n] as i32
}
}