-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0091_Decode_Ways.java
More file actions
31 lines (28 loc) · 828 Bytes
/
0091_Decode_Ways.java
File metadata and controls
31 lines (28 loc) · 828 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
/*
* 91. Decode Ways
* Problem Link: https://leetcode.com/problems/decode-ways
* Difficulty: Medium
*
* Solution Created by: Muhammad Khuzaima Umair
* LeetCode : https://leetcode.com/mkhuzaima/
* Github : https://github.com/mkhuzaima
* LinkedIn : https://www.linkedin.com/in/mkhuzaima/
*/
class Solution {
public int numDecodings(String s) {
int dp1 = s.charAt(s.length()-1) == '0' ? 0: 1;
int dp2 = 1; // empty string
for (int i = s.length()-2; i >= 0; i--) {
int current = 0;
if (s.charAt(i) != '0') {
current += dp1;
if (Integer.parseInt(s.substring(i, i+2)) <= 26) {
current += dp2;
}
}
dp2 = dp1;
dp1 = current;
}
return dp1;
}
}