Skip to content

Commit

Permalink
Day 106
Browse files Browse the repository at this point in the history
  • Loading branch information
Jagannath8 authored Apr 16, 2024
1 parent c0fd995 commit bd0b403
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Ways to Decode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
public class Solution {
public int numDecodings(String A) {
int n = A.length();
long[] dp = new long[n + 1];
dp[0] = 1;
if (A.charAt(0) != '0')
dp[1] = 1;
else
return 0;
for (int i = 1; i < n; i++) {
if (A.charAt(i) != '0')
dp[i + 1] = dp[i];
int num = Integer.parseInt(A.substring(i - 1, i + 1));
if (num >= 10 && num <= 26)
dp[i + 1] += dp[i - 1];

dp[i + 1] %= 1000000007;
if (A.charAt(i) == '0' && (num > 26 || num < 10))
return 0;
}
return (int) dp[n];
}
}

0 comments on commit bd0b403

Please sign in to comment.