Skip to content

Commit cd26978

Browse files
authored
Create 0091_Decode_Ways.java
1 parent dcc90fe commit cd26978

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

0091_Decode_Ways.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// id: 91
2+
// Name: Decode Ways
3+
// link: https://leetcode.com/problems/decode-ways
4+
// Difficulty: Medium
5+
6+
class Solution {
7+
public int numDecodings(String s) {
8+
int dp1 = s.charAt(s.length()-1) == '0' ? 0: 1;
9+
int dp2 = 1; // empty string
10+
11+
for (int i = s.length()-2; i >= 0; i--) {
12+
int current = 0;
13+
if (s.charAt(i) != '0') {
14+
current += dp1;
15+
16+
if (Integer.parseInt(s.substring(i, i+2)) <= 26) {
17+
current += dp2;
18+
}
19+
}
20+
dp2 = dp1;
21+
dp1 = current;
22+
}
23+
return dp1;
24+
}
25+
}

0 commit comments

Comments
 (0)