-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
28 lines (27 loc) · 739 Bytes
/
Copy pathsolution.java
File metadata and controls
28 lines (27 loc) · 739 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
// 91. Decode Ways
// https://leetcode.com/problems/decode-ways/
// Medium | Java | Accepted 2026-01-15
// Runtime 1 ms | Memory 43.2 MB
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 j = 2; j<=s.length(); j++)
{
if(s.charAt(j-1)!='0')
{
dp[j]+=dp[j-1];
}
if(Integer.parseInt(s.substring(j-2, j))>=10 && Integer.parseInt(s.substring(j-2, j))<=26)
{
dp[j]+=dp[j-2];
}
if(dp[j]==0)
{
return 0;
}
}
return dp[s.length()];
}
}