We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent dcc90fe commit cd26978Copy full SHA for cd26978
0091_Decode_Ways.java
@@ -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