Skip to content

Commit f5804fa

Browse files
authored
Merge pull request neetcode-gh#844 from notauserx/Create-91-Decode-Ways.cs
Create 91-Decode-Ways.cs
2 parents 194e193 + d6cdbda commit f5804fa

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

csharp/91-Decode-Ways.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class Solution {
2+
public int NumDecodings(string s) {
3+
List<int> dp = Enumerable.Repeat(1, s.Length + 1).ToList();
4+
5+
for(var i = s.Length - 1; i >= 0; i--) {
6+
if (s[i] == '0')
7+
dp[i] = 0;
8+
else
9+
dp[i] = dp[i + 1];
10+
11+
if (i + 1 < s.Length && (
12+
s[i] == '1' ||
13+
(s[i] == '2' && "0123456".Contains(s[i + 1]))
14+
))
15+
dp[i] += dp[i + 2];
16+
}
17+
18+
return dp[0];
19+
}
20+
}

0 commit comments

Comments
 (0)