NeetCode is also good, however, this is a simplified version with a dp array, and coming up with this solution in an unseen problem is very difficult. Please develop the understanding with recursion + memoization as explained by neetcode, it will be very helpful long term.
Hey nikhil, first of all thanks for your top notch leetcode video solutions. Can you please make one video that should provide roadmap specific to your uploaded playlists/videos. This really make difference and increase watch count.
Thank you very much for the detailed explanation. instead of dp[], we could use 2 place holders to keep track of previous sum right? all we care is i-1 and i-2 can be tracked as prev1 and prev2.
"dp[0] = 1 because there is 1 way to decode it, which is not doing anything"... this is not doing anything but still counted as 1 way????? kinda contradict when its only counted as 1 if there is something to decode, which is why when digit is 0, there is 0 way to decode.. but when digit is empty which is at 0 index, now its counted as 1.. this is hard to understand..
also in counting stair problem there was a base condition where when you are standing on 0th stairs, theres only 1 way to reach it that is by doing nothing. so dp[0] = 1 😂😂😂 man its so confusing
@@GmHighlight12 It does pass all test cases. I tore the code to determine the breakage point. Looks like you may be hinting at the test case "10". Is that the one ?
The issue stems from how you're extracting single and double-digit numbers using stoi and substr. Here's a breakdown of the problem and the solution: Problem: Incorrect Substring Extraction: The substr(i-1, i) in stoi(s.substr(i-1, i)) doesn't extract a single digit. Instead, it extracts a substring from index i-1 up to (but not including) index i. This means you're always getting a two-character substring (except for the last iteration). Unnecessary stoi: You don't need stoi to check if a single digit is between 1 and 9. A simple character comparison is more efficient. tested & working on lc class Solution { public: int numDecodings(string s) { int n = s.length(); vector dp(n + 1); dp[0] = 1; // Empty substring has one way to decode dp[1] = (s[0] == '0') ? 0 : 1; // First digit can't be '0' for (int i = 2; i = 1 && single_digit = 10 && double_digit
This is insane, i wasnt able to understand the neetcode solution, but i am able to understand this! Thanks
NeetCode is also good, however, this is a simplified version with a dp array, and coming up with this solution in an unseen problem is very difficult.
Please develop the understanding with recursion + memoization as explained by neetcode, it will be very helpful long term.
@@thisissheraf2633 Agree!
Thanks for detailed explanation Nikhil. Really appreciate it.
Pushing the algorithm
This man is genius
bro who would be able to come up with this solution if he has never seen it before in an interview? Crazy man
Hey nikhil, first of all thanks for your top notch leetcode video solutions. Can you please make one video that should provide roadmap specific to your uploaded playlists/videos. This really make difference and increase watch count.
I have created some playlists, but yes...can define a better roadmap too. Will work on it. Thanks. :)
Thanks bro! Neat and clear explanation.
Thanks brother, amazing explanation
Thank you very much! You were able to simplify the problem for me and now I actually understand it
always good explanation, consistent and patient with no hurry, cool. 😃
very very perfect solution....i love this explanation...u made it very easy to understand for beginners...thank you so much sir
this video cleared my doubts , tqsm
Best explanation
Nikhil you make things so simple. I really wish you conducted some course in DSA. Thank You.
very well explained
Thank you very much for the detailed explanation. instead of dp[], we could use 2 place holders to keep track of previous sum right? all we care is i-1 and i-2 can be tracked as prev1 and prev2.
That's a great idea! Will save you space.
legend
"dp[0] = 1 because there is 1 way to decode it, which is not doing anything"... this is not doing anything but still counted as 1 way????? kinda contradict when its only counted as 1 if there is something to decode, which is why when digit is 0, there is 0 way to decode.. but when digit is empty which is at 0 index, now its counted as 1.. this is hard to understand..
also in counting stair problem there was a base condition where when you are standing on 0th stairs, theres only 1 way to reach it that is by doing nothing. so dp[0] = 1 😂😂😂 man its so confusing
What if String length only 2 this code will failed to execute
I tested with string length as 2. It works. Can you share specific examples that failed for you ?
Try to run this code on leetcode all test cases won’t pass
@@GmHighlight12 It does pass all test cases.
I tore the code to determine the breakage point. Looks like you may be hinting at the test case "10". Is that the one ?
no it will work
instead of writing dp[1] like that.,,add this check
if(s.charAt(0)=='0'||s.length==0||s==null){
return 0;
}
class Solution {
public:
int numDecodings(string s) {
vector dp(s.length() + 1);
dp[0] = 1;
dp[1] = (s.at(0) == '0')?0:1;
for(int i = 2; i= 1) {
dp[i] += dp[i-1];
}
if (double_digit >= 10 && double_digit
The issue stems from how you're extracting single and double-digit numbers using stoi and substr.
Here's a breakdown of the problem and the solution:
Problem:
Incorrect Substring Extraction: The substr(i-1, i) in stoi(s.substr(i-1, i)) doesn't extract a single digit. Instead, it extracts a substring from index i-1 up to (but not including) index i. This means you're always getting a two-character substring (except for the last iteration).
Unnecessary stoi: You don't need stoi to check if a single digit is between 1 and 9. A simple character comparison is more efficient.
tested & working on lc
class Solution {
public:
int numDecodings(string s) {
int n = s.length();
vector dp(n + 1);
dp[0] = 1; // Empty substring has one way to decode
dp[1] = (s[0] == '0') ? 0 : 1; // First digit can't be '0'
for (int i = 2; i = 1 && single_digit = 10 && double_digit
@@jst8922 thanks man.
@@tamils12345 Bro neenga final year ah bro
@@imdarkseid2343 no. Working