Skip to content

Commit

Permalink
Merge pull request neetcode-gh#844 from notauserx/Create-91-Decode-Wa…
Browse files Browse the repository at this point in the history
…ys.cs

Create 91-Decode-Ways.cs
  • Loading branch information
Ahmad-A0 authored Aug 17, 2022
2 parents 194e193 + d6cdbda commit f5804fa
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions csharp/91-Decode-Ways.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public class Solution {
public int NumDecodings(string s) {
List<int> dp = Enumerable.Repeat(1, s.Length + 1).ToList();

for(var i = s.Length - 1; i >= 0; i--) {
if (s[i] == '0')
dp[i] = 0;
else
dp[i] = dp[i + 1];

if (i + 1 < s.Length && (
s[i] == '1' ||
(s[i] == '2' && "0123456".Contains(s[i + 1]))
))
dp[i] += dp[i + 2];
}

return dp[0];
}
}

0 comments on commit f5804fa

Please sign in to comment.