Leetcode 91. Decode Ways

preview_player
Показать описание
Leetcode 91. Decode Ways

My contact details

You can practice for coding interview from here

I am Mohammad Fraz , a final year Engineer at DTU and I create content for Coding and technical interviews for FAANG. I explain the intuition to solve Data Structure and Algorithm Questions from leetcode and other platforms. I also cover interview experiences for FAANG and other tech giants. You will also find lectures on the frequently asked interview questions.
Рекомендации по теме
Комментарии
Автор

This is the best explanation of decode ways available..Thanks

ruhinapatel
Автор

Without using any DP array (SPACE OPTIMIZED) : using two variables for two states !

class Solution {
public:
int numDecodings(string s) {
int n = s.size();
int a = 1;
int b = 1;
for(int i=n-1; i>=0; i--)
{
int op1 = s[i] - '0';
int op2 = 0;
int cur = 0;
if(i<n-1) op2 = op1*10 + s[i+1] - '0';
if(op1 > 0)
{
cur+=b;
if(op2 > 0 and op2 <= 26) cur+=a;
}
a = b;
b = cur;
}
return b;
}
};

yagniktalaviya
Автор

thank you so much! Took some 3 hours to figure it out the recurrence relation by myself, but this was so better

sanjayagamamidi
Автор

Instead of if(op1&&op2>0&&op2<=26) we can also use if(way2>=10&&way2<=26)

nehaafreen
Автор

@Fraz: Did the same thing is Java but the test case "10" fails as the code gives output as 0 instead of 1.

rajrajeshwarsinghrathore
Автор

Are there some problems where memoization doesnt works or gives TLE and we need to compulsorily do with bottom up dp? And even i==s.size() will work.

palakkotwani
Автор

Hi, I just heard about The Myntra Hackathon for girls. Can you guys please do a video about that in detail?

vaishnavigupta
Автор

I dont have any projects can u tell me easy way to make a good project within 2 weeks for internships in August (i am in 2 year NSUT)

tarun_