Possible words from Phone Digits | letter combination of a Phone number | Decoding Recursion
Possible words from Phone Digits | letter combination of a Phone number | Decoding Recursion
Here in this video, we have solved a problem letter combination of a phone number. Watch it till the end, to understand it completely. Also share it with your friends if you find these lectures helpful. Problem link: https://practice.geeksforgeeks.org/pr…
You can use my code NISHANT to get 15% Off on all GFG courses.
✨tags ✨ Where to learn dp where to learn graphs where to learn dsa how to start programming how to start coding where to learn trees what is memoisation what is tabulation what are graphs where to learn Operating systems where to learn dbms where to learn oops where to learn computer networks where to learn low level design where to learn cs fundamentals Sanchit jain Gaurav sen Gate smashers Placement guide How to start programming where to learn cpp where to learn python where to learn javascript where to learn java Placement guide Nishant Chahar Placement Guide languages to learn resources to learn data structures Projects development AR/VR Blockchain Machine learning Deep Learning Software developer engineer , Side projects , Importance of side projects , Machine Learning Engineer, How to become a machine learning engineer , Associate engineer Data structures Algorithms College Life, College, Memories Fun Fests Chill Enjoy IITD Mood indigo rendezvous IIT NSUT Moksha DTU Engifest Bits Bitsgoa Namaste Javascript Namaste Javascript Akshay Saini Apna college Apna college c++ apna college DSA Where to learn dp where to learn graphs where to learn dsa how to start programming how to start coding where to learn trees what is memoisation what is tabulation what are graphs where to learn programming how to start coding where to learn coding where to learn DSA resources to learn programming how to crack amazon how to crack placement blockchain what is blockchain blockchain technology blockchain technology explained blockchain explained blockchain technology in hindi web development roadmap roadmap to learn web development android development roadmap MERN stack roadmap machine learning roadmap roadmap to learn machine learning roadmap for deep learning roadmap for 2nd years roadmap for opensource roadmap for ios development roadmap for deep learning roadmap to learn DSA
Content
0 -> Hi guys welcome to code in 10. I am Nishant Chahar
2.392 -> So in today's video we gonna talk about this question, the name is "possible words from a phone digit"
7.48 -> Now what is given in that
9.018 -> You will be given an array
10.725 -> In which you will be told that
12.151 -> Which digits are used here
14.702 -> Which digits are pressed here
16.029 -> We have to give output
17.187 -> Which words were possible
18.647 -> If you are 90's kid
20.907 -> If you are born in 2000,2001, 2002, 2003,2004,2005
24.052 -> So you must have played with this phone
25.936 -> Nokia's phone used to come
27.697 -> Reliance phones used to come
28.505 -> In those, there used to be nice games
29.515 -> Okay
30.229 -> There used to be a bounce game in Nokia's phones
32.02 -> So when we used to text in that
34.112 -> So we will have to press a keyword 2-3 times
36.469 -> Like if We want to write c
38.24 -> So we had to press 2 button 3 times
40.901 -> Similarly if we want to write z
43.153 -> We had to press 9 button four times
45.023 -> It used to be very difficult
46.13 -> So we just have to tell
47.992 -> How many possible words can be made
49.109 -> We will take a small example
50.573 -> We will solve for n= 2
52.89 -> After that we will write its code
55.77 -> Then we will explain you
56.69 -> So lets start the video without any delay
58.358 -> Lets consider
59.898 -> That we have n =2
62.714 -> The array that you are given
64.099 -> In that you are given 2,4
66.435 -> So lets paste that keyboard here
69.536 -> Okay
70.211 -> So if you see on this keyboard
72.357 -> This is abc
74.445 -> And on 4 it is ghi
76.581 -> So we have to tell that how many multiple words are possible in it
81.128 -> So if you see
82.597 -> If we will press 2
83.921 -> we have 3 options
85.029 -> A, B and C
87.146 -> Okay
88.019 -> First time we pressed 2, so any one button can be used from them
90.778 -> We again have 3 options
92.408 -> either we will use g or h or i
96.763 -> Similarly here g, h and i
99.524 -> Similarly g, h and i
103.635 -> It will be made separately for everyone
105.062 -> in one, it will be AG
106.267 -> in one it will be AH
109.582 -> And in one, it will be AI
112.432 -> Okay
113.236 -> Similarly same will happen for B
114.78 -> And for C also
115.6 -> So total we will
117.501 -> 3+3+3 that is 9
120.621 -> Okay
122.103 -> So you have to do nothing
123.309 -> The basic subsets and subsequences that we are doing till now
127.454 -> What we do is we tell recursion
128.921 -> That I have done my work on this position
132.467 -> Bring all the that can be possible from the further
136.468 -> And tell me how many answers can be there
138.685 -> Here also we will do the same thing
140.846 -> We will say that in a loop
142.331 -> Currently I am at 2
147.517 -> So I will say
149.088 -> That I have abc in 2
151.525 -> So first bring on a
153.975 -> Make a temporary
155.949 -> And tell him that I have added A in my current string
158.975 -> You go. If I will press 4
161.382 -> How many multiple answers can come. Bring me all of that
163.755 -> So it will bring AG
165.5 -> It will bring AH
167.367 -> and AI
168.692 -> Then we will say
169.542 -> A, your work has done
170.845 -> Now lets do B in place of A
172.719 -> And then we will say B to do the same work
174.954 -> And likewise do C
176.88 -> Then we will ask C to do the same work
178.581 -> In this way all of our strings will be generated
182.481 -> Okay
183.971 -> I hope you understood a little bit
185.308 -> What we guys do is lets take a big example
187.228 -> Try solving that
188.104 -> So what we said, we took n=3
190.211 -> We said
191.244 -> if we are given 2,3,4
193.584 -> Then tell what will be generated
195.452 -> 2 has ABC
196.957 -> 3 has DEF
200.77 -> And 4 has GHI
204.251 -> So now what we will do
205.77 -> We have 3 places
206.829 -> First we will say add all that are possible in 2
208.914 -> Once put A, once put B, once put C
211.526 -> Now tell A and bring second place fill
214.07 -> We have to see 3 at second place what is there on 3
216.459 -> On 3 we can put DEF
217.932 -> Once we will do AD, once AE, once AF
223.506 -> After happening of this, We will say the Ad happened
225.913 -> one 3rd place left
227.358 -> go on 3rd place
229.368 -> And do your further work
231.953 -> What it will do on 3rd place
233.436 -> It will see on 4th that GH has come
235.237 -> like lets take f
236.572 -> What it will say
237.728 -> this became AF
239.746 -> Put G in it
241.073 -> AF happened, now put H in it
243.007 -> AF happened, now put I in it
245.035 -> So doing like this many will be made
247.937 -> If you will try making
249.343 -> Just go on index i
251.424 -> try putting all the possible value there
254.749 -> And tell recursion that bring all after this
258.414 -> Okay
259.151 -> We have done the small calculations that we had to do
261.442 -> No we will apply recursive call
262.646 -> You bring your answer and give us
264.035 -> Our work is done
264.941 -> Now lets write code
266.447 -> We will solve it with code
268.353 -> It will be more clear to you
269.995 -> So what we do first
271.715 -> I have put all of my possible keys here for zero
274.231 -> There is no zero here
275.963 -> But because all of our indexes start from 0
279.41 -> So for simplicity, here it is 1,2,3,4,5,6,7
284.242 -> This is 8,9
285.344 -> No what we do lets make our vector
287.185 -> In which we have to give answer
288.378 -> We will make that same helper function
291.972 -> In it we will also say
295.054 -> That give me your array
296.709 -> Give me size of array
299.389 -> Okay
300.495 -> Let me make a temporary string
302.188 -> Other than that let me make index i
304.426 -> Now that same base case. If the value of i become equal to n
308.416 -> So say
309.791 -> push back in answer
311.421 -> my current temporary string
314.783 -> and return from here and your work has done
317.539 -> Now next we have to write this loop
319.846 -> What we have to say in the loop
321.423 -> That start j from 0
324.325 -> And this is in the keys
327.784 -> What we have to do
329.142 -> That until j's value to keys a of i is less than, keep doing j++
338.986 -> Okay lets understand once
340.321 -> That what is this a of i's value
342.506 -> a of i's value is what we are given here
344.112 -> So it is 2
345.881 -> So what will be the value at 2 of keys, it will be ABC
348.17 -> Only what you have to do, the size of ABC
351.443 -> j should always be smaller than ABC's size until we have to run the code
355.893 -> Okay understood
356.899 -> Now we have to do nothing
358.171 -> Call the helper function
359.455 -> Tell it that this is your array
361.962 -> This is n
363.406 -> Give us by adding in temporary string
366.333 -> What we have to do with keys
367.925 -> a of i's
369.655 -> a of i
371.296 -> After that what we have to do j
372.638 -> , will call i++
374.883 -> Now what is this
378.244 -> When we will code,I will show you then
380.295 -> This means a of i will be ABC
386.398 -> If we take j of ABC as 0
388.543 -> So the value will be a
389.95 -> We said that add a in temporary
392.844 -> Then call the recursion
394.388 -> What was index ?
396.902 -> It was 0
398.611 -> Do 0 as 1
399.878 -> And after that do sum
401.223 -> We have to do nothing. Our code is done
403.233 -> Now we will tell helper
405.158 -> Take a and go
408.094 -> Take n
409.236 -> Take temporary empty string
411.302 -> Take index 0
412.758 -> Return my answer
414.484 -> Work is done
415.458 -> Lets run it now
416.745 -> So it is right
421.877 -> Submit it
423.56 -> This is submitted
429.454 -> Okay now lets try to understand
431.319 -> So now what is happening
434.474 -> The value of a is 2,4
436.962 -> And the value of n is 2
439.247 -> No what we do
440.386 -> empty the temporary
442.141 -> And currently our index is i = 0
444.202 -> So what we said
445.828 -> That called the recursion
447.04 -> Here it became 2
448.541 -> This became our 2
450.11 -> Then we called the recursion
452.89 -> This is our array a, this is n, this temporary is currently empty
455.852 -> And the value of i is 0
456.887 -> We said if the value of i is equal to n, no
458.678 -> n is 2
459.996 -> So it will not go in the if
461.398 -> It will directly come to for loop
462.545 -> In for loop the value of j is 0
463.951 -> Okay
464.845 -> And now we will see
466.737 -> Keys a of i
467.799 -> Okay
468.638 -> What is keys a of i
469.943 -> A of i is
471.703 -> We got value of a of i as 2
474.331 -> Okay
475.556 -> So now we will say that the element that is on second position at keys
478.094 -> Bring that
478.963 -> We have 0,1,2 at second
481.628 -> At 2 we have ABC
482.655 -> We said what is the size of ABC. The size of ABC is 3
485.709 -> So what we will do until j is less than 3
488.025 -> Keep running it until then
489.441 -> And do j++
490.688 -> What we told helper
492.618 -> Keep a
494.022 -> Take n again
494.764 -> And temp was empty
495.857 -> And in empty keys of a of i
499.098 -> We saw here a of i is 2
500.336 -> So what will happen of keys 2
502.024 -> After this, next is j
504.6 -> j is 0
505.124 -> So ABC is a string itself
508.401 -> We know it is also 0,1,2
510.755 -> So we said its zeroth element
513.331 -> That is a
515.73 -> So add with temp
517.073 -> So here a will be added
518.576 -> And do recursion call here
520.424 -> Okay so now it will call recursion
522.03 -> Okay
523.404 -> Like we saw here
524.656 -> Now it will call the recursion
525.819 -> Now go next
527.53 -> Your value of i has become 1
529.593 -> and 1 has been put on 4
531.213 -> On 4 we will see GHI is there
533.146 -> i == n didn't happened
535.602 -> So it will not go in the if
536.673 -> Here it will check again
538.013 -> keys a of i. A of is is 4
539.761 -> Okay
540.707 -> GHI
541.702 -> GHI's size is also 3
543.133 -> So it will be 3
543.796 -> This will do the same again. it will say helper a,n
546.051 -> We had a in temp
548.133 -> Keys 4,0
550.516 -> What is 4,0
551.805 -> It will say AG
553.248 ->
555.319 -> Do i as +1
557.583 -> When we will +1, it will be 2 in place of 1
561.345 -> And again call the recursion
562.995 -> Then it will call the recursion
564.512 -> It will see that the value of i that has become 2
567.745 -> 2 and 2 became same
569.593 -> So it will go and say add Ag inn the answer
572.159 -> it will add AG in answer
573.586 -> And come back on recursion
576.137 -> It will come back on recursion, so the value in temporary will be A
579.806 -> G will not be there
580.87 -> It will go again
581.841 -> j will be incremented
583.685 -> It will be 1 from 0
585.028 -> After becoming 1 we will see that it is 1 here
587.477 -> AH will come
588.803 -> AH will go above again
590.505 -> Because it will call the recursion
592.978 -> Okay
593.831 -> In this way it will keep calling the recursion
596.092 -> It will come above and call the recursion
597.752 -> Then it will call the recursion
599.088 -> After happening of this, it will call the recursion again above
601.743 -> From where we started
602.756 -> Then it will come on B
604.195 -> It will keep doing the same thing
606.129 -> And in this way all of our possible ways will be generated
609.618 -> And we will get our answer
612.137 -> We have to do nothing
613.747 -> Just go to understand this code
615.676 -> Pick up pen and paper
617.005 -> Try making recursive tree
618.741 -> I have made
622.695 -> We made recursive tree here
624.543 -> Okay
626.12 -> Lets try making it again
627.591 -> Now let me tell it how it will be made
630.529 -> We have 3 options in empty
633.786 -> We said either add a or b or c
637.443 -> Then we said in a
639.224 -> do ag
640.941 -> and ah
641.861 -> and ai
642.659 -> Or do bg, bh, bi
647.795 -> do cg, ch, ci
652.141 -> And these are all our possible outcomes
654.983 -> We will return everything
657.499 -> This is it
658.841 -> It was this much
660.021 -> It looks like a hard question in writing
661.975 -> But very simply from one line of recursion
665.272 -> We did only until one line
666.669 -> We wrote very small code
668.01 -> It is solved
669.248 -> Try solving and if you do not understand anything, do write in the comments section