Skip to content

Commit 9ff9dda

Browse files
authored
Create heozeop.cpp
1 parent dd8141d commit 9ff9dda

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

decode-ways/heozeop.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Time Complexity: O(n)
2+
// Spatial Complexity: O(n)
3+
4+
class Solution {
5+
public:
6+
int numDecodings(string s) {
7+
if(s.length() < 1 || s[0] == '0') {
8+
return 0;
9+
}
10+
11+
vector<int> dp(s.length() + 1, 0);
12+
dp[0] = dp[1] = 1;
13+
if(s[1] == '0') {
14+
dp[1] = 0;
15+
}
16+
17+
int prev,pprev;
18+
for(int i = 2; i <= s.length(); ++i) {
19+
prev = s[i - 1] - '0';
20+
if (prev <= 9 && prev > 0) {
21+
dp[i] += dp[i-1];
22+
}
23+
pprev = (s[i - 2] - '0') * 10 + prev;
24+
if(pprev <= 26 && pprev > 9) {
25+
dp[i] += dp[i-2];
26+
}
27+
}
28+
29+
return dp[s.length()];
30+
}
31+
};

0 commit comments

Comments
 (0)