Leetcode Decode Ways || Intuition + Code + Explanation

preview_player
Показать описание
A message containing letters from A-Z can be encoded into numbers using the following mapping:

'A' - "1"
'B' - "2"
...
'Z' - "26"
To decode an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For example, "11106" can be mapped into:

"AAJF" with the grouping (1 1 10 6)
"KJF" with the grouping (11 10 6)
Note that the grouping (1 11 06) is invalid because "06" cannot be mapped into 'F' since "6" is different from "06".

Given a string s containing only digits, return the number of ways to decode it.

The answer is guaranteed to fit in a 32-bit integer.



Example 1:

Input: s = "12"
Output: 2
Explanation: "12" could be decoded as "AB" (1 2) or "L" (12).
Рекомендации по теме
Комментарии
Автор

Mam thank you so much your dp lectures are better than many paid courses.
U r explanation is so simple and easy to understand 😊😊

vishnuvardhanreddy
Автор

Nice explanation! Thank you.
Modified your code(Java) for constant space:

class Solution {

public int numDecodings(String s) {
if(s.isEmpty() || s.charAt(0) == '0') return 0;
int twoStepsBack = 1;
int oneStepsBack = 1;
for(int i = 2; i <= s.length(); i++){
char currentChar = s.charAt(i - 1);
int curr = 0;
if(currentChar != '0'){
curr = oneStepsBack;
}
char previousChar = s.charAt(i - 2);
if((previousChar == '1')
|| (previousChar == '2'
&& (currentChar >= '0' && currentChar <= '6'))) {
curr += twoStepsBack;
}
twoStepsBack = oneStepsBack;
oneStepsBack = curr;
}
return oneStepsBack;
}
}

VinodMuda
Автор

I paused the video at 18:39 to subscribe your channel, thanks for the easy explanation

AbdulRehman-uinj
Автор

Clear explanation, thank you. Also your videos are very crisp and clear, please make more vidoes, finding quite helpful.

keerthana
Автор

Very Well Explained Thank you very much

SHANMUKHASHARMA-sh
Автор

This was such a clear explanation video. Thank you!

nivedithabaskaran
Автор

Clear & Nice explanation!
Thank you very much ma'am!

TheElevatedGuy
Автор

Great explanation ma'am, but I think there is a small mistake in it, we can decode "110" in 1 way only (1, 10), not 2 ways [21:30]
1, 1, 0 is not a valid decoding

biswajitsaha
Автор

awesome explanantion
just solved it after getting the intuition

smile
Автор

Hi @code with Alisha,

it was such a nice explanation, you efforts were visible in video. However there was a cut in video when you just started tabulation part and then suddenly you lowered your voice, was that because you were recording at night ?.
haha 😁, if that was so, that was such a cute moment 🤣.

sakshamgupta
Автор

Hey, Can you make a video, how your coding journey start? Where to start? Which platform to do first?

shashankpal
Автор

There was a movavi editor plus banner which flashed in intervals in the video.

himanshupandey
Автор

getting memory limit exceeded while using recursion + memoization

pranjalshinde
Автор

Ma'am please tell that can I solve this question using BFS or not?
I write a code myself using queue for BFS but that's running only for few cases so please tell..

lalitagarwal
Автор

why you have taken previous two state i.e dp[i] += dp[i-2] . why dp[i-2] ? And why you have take dp[0] = 1;

aakashgoswami
Автор

You mentioned that you'd tell about why dp[0] = 1. Could you explain that, please?

raj_kundalia
Автор

except striver, no youtuber can write recursive solution by themselves

vishalagarwal
Автор

class Solution {
public:
int numDecodings(string s) {
int i, n=s.length();
if(s.length()==0||s[0]=='0')
return 0;
vector<int>dp(n+1, 0);
dp[0]=1;
dp[1]=1;

for(i=2;i<=n;i++){
if(s[i-1]>='1'&&s[i-1]<='9')
dp[i]=dp[i-1];
if(s[i-2]=='1')
dp[i]+=dp[i-2];
else
dp[i]+=dp[i-2];
}
return dp[n];
}
};
this code is not satisfying all the test cases..please check

jayasatwik
Автор

Great work 👏 check your LinkedIn request 👏

ramashishrural