LeetCode #91: Decode Ways | Facebook Coding Interview

preview_player
Показать описание

Facebook Coding interview question - Decode Ways (Leetcode)
**** Best Books For Data Structures & Algorithms for Interviews:

#Facebook #CodingInterview #LeetCode #DecodeWays #Google
Рекомендации по теме
Комментарии
Автор

This question was also asked to me in my Amazon interview recently for the role of SDE

poojaagarwal
Автор

Watched both versions, counterintuitive use of vals/counts.

annawilson
Автор

just amazing... one of the best explanation :)

PradeepKumar-sowq
Автор

class Solution {
public:
int numDecodings(string s) {
int n = s.size();
if (n == 0 || s[0] == '0') return 0; // If the string is empty or starts with '0', it can't be decoded

vector<int> dp(n + 1, 0); // dp[i] represents the number of ways to decode s[0:i]
dp[0] = 1; // Base case: one way to decode an empty string
dp[1] = 1; // One way to decode the string if the first character is valid (non-zero)

for (int i = 2; i <= n; i++) {
// Check single-digit decode possibility
if (s[i - 1] != '0') {
dp[i] += dp[i - 1];
}
// Check two-digit decode possibility
int twoDigit = stoi(s.substr(i - 2, 2));
if (twoDigit >= 10 && twoDigit <= 26) {
dp[i] += dp[i - 2];
}
}

return dp[n]; // Number of ways to decode the full string
}
};

abhisheksrivastava
Автор

I haven't Get How Val1 Val2 is working here..You explains Nice Sir...but this the single time i get confused

uavishal
Автор

This approach is so good bro, but a test case like "100" might not get a right answer, I guess. Just pointing out for greater good. Thanks again for your efforts to help people. Appreciate it.

balajik
Автор

0 understanding from this video time wasted

ravifoodvlogs