Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2dded50

Browse filesBrowse files
authored
Create 18_decodeWays.cpp
1 parent 03d4b88 commit 2dded50
Copy full SHA for 2dded50

File tree

Expand file treeCollapse file tree

1 file changed

+17
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+17
-0
lines changed
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int memo[100] = {};
4+
int numDecodings(const string& s) {
5+
return dp(s, 0);
6+
}
7+
int dp(const string& s, int i) {
8+
if (i == s.size()) return 1;
9+
if (memo[i] != 0) return memo[i];
10+
int ans = 0;
11+
if (s[i] != '0') // Single digit
12+
ans += dp(s, i+1);
13+
if (i+1 < s.size() && (s[i] == '1' || s[i] == '2' && s[i+1] <= '6')) // Two digits
14+
ans += dp(s, i+2);
15+
return memo[i] = ans;
16+
}
17+
};

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.