-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_0091.java
More file actions
25 lines (21 loc) · 826 Bytes
/
_0091.java
File metadata and controls
25 lines (21 loc) · 826 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
package com.github.aditya;
public class _0091 {
// 1 ms, faster than 98.62%, memory 40.8 MB, less than 89.43%
// DP - Time Complexity O(n) and Space Complexity O(n)
class Solution {
public int numDecodings(String s) {
int[] dp = new int[s.length() + 1];
dp[0] = 1;
dp[1] = s.charAt(0) == '0' ? 0 : 1;
for (int i = 2; i <= s.length(); i++) {
int oneLengthString = Integer.parseInt(s.substring(i - 1, i));
int twoLengthString = Integer.parseInt(s.substring(i - 2, i));
if (oneLengthString > 0)
dp[i] += dp[i - 1];
if (twoLengthString <= 26 && twoLengthString >= 10)
dp[i] += dp[i - 2];
}
return dp[s.length()];
}
}
}