Skip to content

Commit

Permalink
Update decode_ways.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmad-A0 authored Dec 22, 2022
1 parent 38088bf commit 53fc96c
Showing 1 changed file with 38 additions and 40 deletions.
78 changes: 38 additions & 40 deletions cpp/neetcode_150/13_1-d_dynamic_programming/decode_ways.cpp
Original file line number Diff line number Diff line change
@@ -1,43 +1,3 @@
/*
Given a string w/ only digits, return # ways to decode it (letter -> digit)
Ex. s = "12" -> 2 (AB 1 2 or L 12), s = "226" -> 3 (2 26 or 22 6 or 2 2 6)
DP: At each digit, check validity of ones & tens, if valid add to # ways
Recurrence relation: dp[i] += dp[i-1] (if valid) + dp[i-2] (if valid)
Time: O(n)
Space: O(n)
*/
/*
class Solution {
public:
int numDecodings(string s) {
if (s[0] == '0') {
return 0;
}
int n = s.size();
vector<int> dp(n + 1);
dp[0] = 1;
dp[1] = 1;
for (int i = 2; i <= n; i++) {
int ones = stoi(s.substr(i - 1, 1));
if (ones >= 1 && ones <= 9) {
dp[i] += dp[i - 1];
}
int tens = stoi(s.substr(i - 2, 2));
if (tens >= 10 && tens <= 26) {
dp[i] += dp[i - 2];
}
}
return dp[n];
}
};
*/

/*
Given a string w/ only digits, return # ways to decode it (letter -> digit)
Ex. s = "12" -> 2 (AB 1 2 or L 12), s = "226" -> 3 (2 26 or 22 6 or 2 2 6)
Expand Down Expand Up @@ -74,3 +34,41 @@ class Solution {
return curr;
}
};

/*
Given a string w/ only digits, return # ways to decode it (letter -> digit)
Ex. s = "12" -> 2 (AB 1 2 or L 12), s = "226" -> 3 (2 26 or 22 6 or 2 2 6)
DP: At each digit, check validity of ones & tens, if valid add to # ways
Recurrence relation: dp[i] += dp[i-1] (if valid) + dp[i-2] (if valid)
Time: O(n)
Space: O(n)
*/
class Solution2 {
public:
int numDecodings(string s) {
if (s[0] == '0') {
return 0;
}

int n = s.size();

vector<int> dp(n + 1);
dp[0] = 1;
dp[1] = 1;

for (int i = 2; i <= n; i++) {
int ones = stoi(s.substr(i - 1, 1));
if (ones >= 1 && ones <= 9) {
dp[i] += dp[i - 1];
}
int tens = stoi(s.substr(i - 2, 2));
if (tens >= 10 && tens <= 26) {
dp[i] += dp[i - 2];
}
}

return dp[n];
}
};

0 comments on commit 53fc96c

Please sign in to comment.