Ep15- Letter Combinations of a Phone Number | DSA | Codes available in description

แชร์
ฝัง
  • เผยแพร่เมื่อ 18 ม.ค. 2025

ความคิดเห็น • 107

  • @arghya_0802
    @arghya_0802 2 ปีที่แล้ว +17

    Summary:
    1. This question uses all the concepts of subsets and combinations we have studied so far.
    2. In this problem, we need to generate all the words possible from the number of keypad given by the user as input
    3. To solve this problem, we firstly need a unordered_map to map all the keypad characters like '2' , '3' , '4' , etc with the letters they represent in the keypad phone. This can be done using unordered_map or vector using its indices
    4. Now we need to create a function help which takes the given string s, ans vector to store all the answers, i to iterate over given string and a temp string to store the current word we are making. Everything is passed by reference to save space.
    5. If i reaches to s.size(), it means we have traversed the entire string, so we put the temp word inside the ans vector and return.
    6. Else we would pick a string str = map[s[i]] . This will represent all the letters represented by i-th character of string s.
    7. We run a for loop for j = 0 till j < str.size() and we add str[j] character inside our temp string and ask Recursion to solve for i+1th character.
    8. After Recursion gives me the answer, we have to backtrack and pop the character we have included so that other characters of string str can now take its place.
    10. Lastly, we return ans vector from our given function.
    Time complexity: O(3^N) [ As most of the letters has 3 choices so we can approximately say time complexity as 3^N]
    Space Complexity: O(N) [ N is the size of the given string. This is because we can only process till i < s.size() , that's why Space Complexity is O(N)]

    • @emtiazahmed5333
      @emtiazahmed5333 2 ปีที่แล้ว +1

      Yes i.. Check its🥰

    • @ranitbandyopadhyay
      @ranitbandyopadhyay 2 ปีที่แล้ว +2

      Space complexity in my opinion is O(9*n) + O(3^n) +O(3^n). The first term is for the dictionary(hashmap), the second term is for the temp variable as there are (3^n) copies of the temp variable and the final term is for the ans that stores all the (3^n) copies of temp variable into it for returning. Check and tell if there is any mistake or not.

    • @emtiazahmed5333
      @emtiazahmed5333 2 ปีที่แล้ว +1

      @@ranitbandyopadhyay @fraz bhaiya pls give me correct space complexity..

    • @arghya_0802
      @arghya_0802 2 ปีที่แล้ว +1

      @@ranitbandyopadhyay What you are saying is not wrong, but we can safely ignore ans vector because it's something asked in the question, so mainly the space Complexity will be the hashMap+ Recursive tree. The hashMap will always be of constant space, it's not something dependent upon n. We will always map the same 26 alphabets that's why, I think, we can safely ignore it. And regarding temp string, we are passing it by reference so that we are not creating unnecessary copies.
      So Time Complexity boils down to Height of the Recursive Tree which is O(N) in my opinion

    • @ranitbandyopadhyay
      @ranitbandyopadhyay 2 ปีที่แล้ว +2

      @@arghya_0802 Actually the thing is that although the recursion stack space is O(N) but inside the recursion, we have a for loop which goes to 3^N for a single number. So although the function calls are O(N) but the number of times the function is called needs also to be taken into account. So although the correct answer is O(N) but the thing is that as each number corresponds to 3-4 characters, so I was thinking it that way. Thanks for the clarification.

  • @sumantsuryawanshi6259
    @sumantsuryawanshi6259 2 ปีที่แล้ว +5

    Thanks for the Lectures Sir, studying recursion has become interesting and easy because of you 🙌🙌

  • @ecs185_shaileshbharti3
    @ecs185_shaileshbharti3 2 ปีที่แล้ว +3

    Earlier i used to just understand those easy factorial type questions but honestly saying now recursion seems very interesting ...
    As always Reach++;
    ✌️💥

  • @avadhutlohar5181
    @avadhutlohar5181 2 ปีที่แล้ว +3

    #EP_15 completed
    Consistency OP 🔥🔥🔥

  • @kirandeepmarala5541
    @kirandeepmarala5541 2 ปีที่แล้ว +1

    Hi Fraz brother..I am following the lectures from starting and your consistency makes everyone of us also consistency..Have participated in the codestudio contest that was organised and that helped me a lot to retify my mistakes and how to think and execute in the limited time..Actually I had a lot of fear while doing Recurssion in the past, but now I can say by following your every lecture very carefully, I am very confident to analyze the recurssion problems...The Best part of your teaching is the intutive way of explaining the things and not leave even a single detail..This Recurssion playlist will act as a holy book down the line for many upcoming students...Really Thanks a lot for everything you are doing and making quality education to reach to all..

    • @LearnYardYT
      @LearnYardYT  2 ปีที่แล้ว

      Thanks a lot 😊, be motivated and keep working ❤️

  • @jayantmishra6897
    @jayantmishra6897 2 ปีที่แล้ว +2

    now we are very comfortable in recursion. and we want another series in dynamic programming please if you can make this then please sir make a series on dynamic programming as of now you had given us best linked list series and this recursion series.please think about it if it is possible then please sir.

  • @vedbhandari7378
    @vedbhandari7378 2 ปีที่แล้ว

    lovely ... .cant get over the Tak tak ..........tak tak tak

  • @manojgollapelli9856
    @manojgollapelli9856 2 ปีที่แล้ว

    I have solved two approaches code, without seeing this video.. You made recursion so simple.. Thank you sir

  • @satyammishra6356
    @satyammishra6356 2 ปีที่แล้ว +1

    You made recursion look so much easier. Awesome video. Keep up the good work 🔥

  • @arghya_0802
    @arghya_0802 2 ปีที่แล้ว +1

    6:24
    We could use a array or vector of size 11. Leaving the indices 0,1 rest all we can use to map the keypads of phone with the letters they represent.
    Also, unordered_map can also be used as we are mapping a integer , string it will be of the format map

    • @emtiazahmed5333
      @emtiazahmed5333 2 ปีที่แล้ว +1

      Yes thank u🥰

    • @arghya_0802
      @arghya_0802 2 ปีที่แล้ว +1

      @@emtiazahmed5333 I have also added the summary. Do check it out. Sorry for being late bro this time

    • @emtiazahmed5333
      @emtiazahmed5333 2 ปีที่แล้ว +1

      @@arghya_0802 ok i will... Check that & no problem.. For the late u give the summary this is important,🥰🥰 thank u once agian

    • @mohdumar6990
      @mohdumar6990 2 ปีที่แล้ว +1

      yeah, we can also use array but no need to use size of 11, we use array with index 0 and minus by 2 (eg -: 2 - 2 => 0 index, 3-2 => 1 index, so on

    • @arghya_0802
      @arghya_0802 2 ปีที่แล้ว

      @@mohdumar6990 Yes sure. For the sake of easiness I proposed that, other than that an array of size 9 will work fine. We just need to do (int) (s[i-2] - '0')

  • @mohdumar6990
    @mohdumar6990 2 ปีที่แล้ว +3

    code using the array -:
    void solve(string s, string arr[], int i, vector &ans, string subStr)
    {
    if(i == s.length()) {
    ans.push_back(subStr);
    return;
    }
    int ch = s[i] - '0';
    string str = arr[ch - 2];
    for(int j = 0; j < str.length(); j++){
    subStr.push_back(str[j]);
    solve(s, arr, i + 1, ans, subStr);
    subStr.pop_back();
    }
    }
    vector combinations(string s){
    vector ans;
    string arr[] = {"abc", "def", "ghi",
    "jkl", "mno", "pqrs",
    "tuv", "wxyz"};
    string subStr;
    solve(s, arr, 0, ans, subStr);
    return ans;
    }

  • @vishalgowrav
    @vishalgowrav 2 ปีที่แล้ว +3

    Episode 15 completed! 🔥
    Letter combination of a phone number(Keypad problem)
    Here, we need to generate all the combination of the number in the keypad
    For each number we will have 3 letters so we are storing the values using a hashmap(unordered set)
    And we are doing our work for the first number and asking recursion to do other task as we have 3 letters we are traversing those letters and calling recursive functions and pushing the answers into temp and doing backtracking step and finding ans and stopping the tree when it reaches bound.

  • @maazulhaque7293
    @maazulhaque7293 2 ปีที่แล้ว

    Lecture 15 Done
    Thanks Fraz bhaiyya

  • @emtiazahmed5333
    @emtiazahmed5333 2 ปีที่แล้ว +2

    I thik aj lecture nahi ayega..😴 finally its..comes thank u bhaiya😍

  • @DPCODE72
    @DPCODE72 2 ปีที่แล้ว

    That is what I had been waiting for this video. Bhaiya!!

  • @uavishal777
    @uavishal777 2 ปีที่แล้ว +1

    i don't know why only this much view on your channel but the way you explain is magnificent 👍👍💪💪... Bhaiya upload more Such playlist.

  • @nithins3895
    @nithins3895 2 ปีที่แล้ว

    i missed the series in middle way , but today im taking leave and watching your videos and making notes out of it

  • @it_08amanagarwal35
    @it_08amanagarwal35 2 ปีที่แล้ว +2

    Excellent session bro now I am feeling u should do backtracking after recursion it will be awesome to be taught from u❤❤❤❤❤❤❤❤❤❤

  • @aasifali9139
    @aasifali9139 2 ปีที่แล้ว +5

    according to me:
    space complexity = O(n) where n is the size of the given string s
    time complexity = O(3^n) where n is the size of the given string s
    btw thanks a lot bhaiya for these awesome lectures. I am loving these.

    • @sandypatel_
      @sandypatel_ ปีที่แล้ว

      some mapping has 4 character so in worst case tc 0(4^n) (for ex : 7--->pqrs 9-->wxyz)

  • @Abhishek-fo3fc
    @Abhishek-fo3fc 2 ปีที่แล้ว +1

    Done Understood ❤️✅
    Thank you bhaiya

  • @chiragkarnwal6740
    @chiragkarnwal6740 2 ปีที่แล้ว +4

    Time complexity:-O(3^n) ignoring 4 letters key 7 and 9

  • @MANOJKUMAR-mz3gv
    @MANOJKUMAR-mz3gv 2 ปีที่แล้ว

    Super simple and straight forward explanation thanks a lot bro

  • @chiragkarnwal6740
    @chiragkarnwal6740 2 ปีที่แล้ว +1

    Thanks for the lectures sir ❤️❤️

  • @dashundev1586
    @dashundev1586 2 ปีที่แล้ว

    enjoying it
    looked tough
    but you solved it very easily and it was very easy to understand
    Space Comp : O(n)
    Time Comp : O(3^n)

  • @charlescoon577
    @charlescoon577 ปีที่แล้ว

    Thank you for doing this series.

  • @sharajbaruah5262
    @sharajbaruah5262 ปีที่แล้ว

    oh my goddd sir loved ur fabulous explanation

  • @ratnasanjay
    @ratnasanjay 2 ปีที่แล้ว

    Thankyou bhaiya for sharing your knowledge for free it was great to learn from you

  • @shakibsiddiqui2999
    @shakibsiddiqui2999 2 ปีที่แล้ว

    Thank You bro for this lecture........i m following your series

  • @afaquejaya2524
    @afaquejaya2524 2 ปีที่แล้ว

    Love the way you explain brother, but if you added excel sheet with
    1. All DSA topic and the related link to your video on it.
    In a basic, mid ,adv level problem and their solution.
    But
    2. Atleast firstly mention all the major topic that you have covered and going to cover. So that it will be easy for us to follow up consistently on regular basis.
    Waiting for that excel sheet 😁

  • @isikamaiti9116
    @isikamaiti9116 2 ปีที่แล้ว

    Your consistency is great .

  • @alien65629
    @alien65629 ปีที่แล้ว

    bhaiya upload the same kind of playlist for the remaining topics of dsa.

  • @anuraggulati2167
    @anuraggulati2167 2 ปีที่แล้ว

    thanks for this question sir 🙌🙌❤

  • @AmanChauhan-hr1wh
    @AmanChauhan-hr1wh ปีที่แล้ว

    you explained it very well ...thansky

  • @HarshRaj-kp7gk
    @HarshRaj-kp7gk 2 ปีที่แล้ว +2

    #we want more questions practice ❤️❤️

  • @mdsufyankhan1029
    @mdsufyankhan1029 2 ปีที่แล้ว

    Recursion made easy , nice explained. ❤️

  • @codencode5546
    @codencode5546 2 ปีที่แล้ว +1

    I think instead of taking a map we can make vector/arr of const Size and pass it to a recursive function. if and only if someone does not know the "map" concept.

  • @dipikabhagat5252
    @dipikabhagat5252 7 หลายเดือนก่อน

    Nice explanation

  • @sagarpokhriyal6926
    @sagarpokhriyal6926 2 ปีที่แล้ว

    Lecture 15 completed 👌💯
    #dsabyfaraz

  • @nithins3895
    @nithins3895 2 ปีที่แล้ว

    Thanks for the video

  • @shivendrayadav6780
    @shivendrayadav6780 2 ปีที่แล้ว

    Awesome job ! ❤️

  • @jivanninawe3190
    @jivanninawe3190 2 ปีที่แล้ว

    Bhai aur kitne problems ahenge samne recursion ke?

  • @pritishpattnaik4674
    @pritishpattnaik4674 2 ปีที่แล้ว

    we can use map of int , string to store the letters with numbers

  • @AtomicAkshay
    @AtomicAkshay 2 ปีที่แล้ว

    Notes:
    Good problem, demonstrates the use of recursion in the real world
    Time Complexity: O(3 ^ n) because the size of the recursion tree increases exponentially
    Space Complexity: O(n) excluding size of result array

  • @BurhanAijaz
    @BurhanAijaz 2 ปีที่แล้ว +1

    Fraz bhai plz reply , what about dsa in c and c++ by abdul bari sir on udemy should i start that, btw i am in my 2nd year ?

    • @LearnYardYT
      @LearnYardYT  2 ปีที่แล้ว

      Bro seriously dekha nahi hai abhi tak.

    • @BurhanAijaz
      @BurhanAijaz 2 ปีที่แล้ว

      @@LearnYardYT ok bro

    • @BurhanAijaz
      @BurhanAijaz 2 ปีที่แล้ว

      @@LearnYardYT abhi tak toh aapka course full follow kar raha hun but demotivate hogaya parsu contest ki wajah se kyunki
      1st question solve hua
      2nd mai tle
      3rd hua hi nahi

    • @BurhanAijaz
      @BurhanAijaz 2 ปีที่แล้ว

      @@LearnYardYT isi liye soncha vo bohut basic se hai aur deep tak hai so i should opt for that

    • @BurhanAijaz
      @BurhanAijaz 2 ปีที่แล้ว

      @@LearnYardYT aur ye aapka chodunga nahi course ye bhi continue karunga parallely

  • @adityarao4157
    @adityarao4157 ปีที่แล้ว

    instead of map we can also use vector of strings

  • @Gorangjain
    @Gorangjain 2 ปีที่แล้ว

    mapping we can use

  • @de_ansh
    @de_ansh 2 ปีที่แล้ว

    I was stuck with my internship and was not able to watch the videos, I was feeling bad for that because you are giving your best just for the sake, thank you so much, I'm going to watch all the videos today.

  • @klvp_techie
    @klvp_techie 2 ปีที่แล้ว

    Logic:
    We have to consider each character from the given input number which can be achieved by the loop and ask the recursion to do the same for the remaining input numbers.
    If the size of the word(here temp variable length) reaches to number of input number then save it in the answer
    Code:
    def combi(i,s,m,temp,ans):
    if i >= len(s):
    ans.append(temp)
    return
    for j in m[s[i]]:
    # combi(i+1,s,m,temp + j,ans) below three lines can be replaced by this single line
    temp += j
    combi(i+1,s,m,temp,ans)
    temp = temp[:-1]
    m = {
    "2": "abc",
    "3": "def",
    "4": "ghi",
    "5": "jkl",
    "6": "mno",
    "7": "pqrs",
    "8": "tuv",
    "9": "wxyz"
    }
    ans = []
    s="23"
    temp = ""
    combi(0,s,m,temp,ans)
    print(ans)

  • @amandixit8342
    @amandixit8342 2 ปีที่แล้ว

    bhaiya mein apke lectures after 15 june se start karunga , kuki abhi xams chal rahe hai back na lag jaye isliye mujhe coding thode time ke side karni padi na chahate hue b , kuki 9 subj + 4labs hai for prep the xam , mera sawal ye hai ki agar mujhe koi difficulties aati hai toh mein live session mein purane lectures ke doubt puch skat hoon na ?

  • @pritishpattnaik4674
    @pritishpattnaik4674 2 ปีที่แล้ว

    My summarization :
    So, here in this solution first I created an unordered_map of char and string and stored all the numbers with letters as shown in the keypad, then I created a helper function the I accessed the numeric characters of string digits , and then by mp I accessed it's string and iterate over the string with a j loop and then fushed the character in my temp string and called the helper function recursively i+1th ele of the digits string and then I backtracked my stemp by popping back the element from temp string
    And for the base condition , if i is equal to exceeds the string size then we will push it back to the main ans vector and return from there
    My code :
    void helper(int i, string &s, string &temp, vector &ans, unordered_map &mp){
    //base cond
    if (i >= s.size()){
    ans.push_back(temp);
    return;
    }

    string str = mp[s[i]];
    for (int j=0 ; j

  • @SunnyKumar-jx2qr
    @SunnyKumar-jx2qr 2 ปีที่แล้ว

    Dictionary is one the data structure which can have key values pair type

  • @comic_forever
    @comic_forever 2 ปีที่แล้ว

    Hii bhaiya, there is also an edge case if string is empty, s=" ", then return {};

  • @bibs24
    @bibs24 2 ปีที่แล้ว

    Like i got stuck in this lecture how it will create combinations with that help function if anyone can break down

  • @mohammedsaqib1250
    @mohammedsaqib1250 2 ปีที่แล้ว +1

    Bhai leetcode par karao na questions

  • @divyanshsagar
    @divyanshsagar 2 ปีที่แล้ว

    Episode 15 done!

  • @KunalKumar-tm1zp
    @KunalKumar-tm1zp 2 ปีที่แล้ว

    time complexity - o(3^n) and space complexity is o(n).

  • @abhishekc3556
    @abhishekc3556 2 ปีที่แล้ว

    🔥🔥

  • @ArunBorse-g2k
    @ArunBorse-g2k ปีที่แล้ว

    Hi the backtrack step is missing in your java code available in github
    public class Solution {
    public ArrayList letterCombinations(String A) {
    ArrayList ans=new ArrayList();
    if(A.length() == 0)
    return ans;
    String temp="";
    HashMap map=new HashMap();
    map.put('0',"0");
    map.put('1',"1");
    map.put('2',"abc");
    map.put('3',"def");
    map.put('4',"ghi");
    map.put('5',"jkl");
    map.put('6',"mno");
    map.put('7',"pqrs");
    map.put('8',"tuv");
    map.put('9',"wxyz");
    helper(0,A,temp,ans,map);
    return ans;

    }
    public void helper(int i,String A,String temp,ArrayList ans,HashMap map)
    {
    if(i==A.length())
    {
    ans.add(temp);
    return;
    }
    char ch= A.charAt(i);
    String str=map.get(ch);
    for(int j=0;j

  • @sushant2287
    @sushant2287 2 ปีที่แล้ว

    Space complexity should be O(length of the input)
    🤔right? can you please tell this in next lecture

  • @hyram8938
    @hyram8938 2 ปีที่แล้ว

    space complexity : logn base 3?

  • @Zaheer2004
    @Zaheer2004 ปีที่แล้ว

    It think height of recursion is equal to the size of string
    Which is s.c O (n)

  • @mridulbagla
    @mridulbagla 2 ปีที่แล้ว

    Space Complexity would be O(n)

  • @Lucifer-xt7un
    @Lucifer-xt7un 2 ปีที่แล้ว +1

    Consistency ++

  • @jayantmittal6665
    @jayantmittal6665 2 ปีที่แล้ว

    2d vector and Hashmaps

  • @yogesh3938
    @yogesh3938 11 หลายเดือนก่อน

    kraxzyyy sir map use kar diya

  • @mukeshkir1991
    @mukeshkir1991 2 ปีที่แล้ว

    can we use this instead of map.....
    string mapping[10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    what do you say ? bhaiya

  • @BurhanAijaz
    @BurhanAijaz 2 ปีที่แล้ว

    Day 15
    tabyat theek thi ?

    • @LearnYardYT
      @LearnYardYT  2 ปีที่แล้ว

      Perfect bro. Bas kuch finish Kiya.

    • @BurhanAijaz
      @BurhanAijaz 2 ปีที่แล้ว

      @@LearnYardYT keep the consistency going, u motivate us as well to stay consistent, and i have been following ur course all alonh

  • @mukeshkir1991
    @mukeshkir1991 2 ปีที่แล้ว

    void printStr(string &digit, string mapping[], string &output, int index, vector &ans)
    {
    // base case
    if (index >= digit.length())
    {
    ans.push_back(output);
    return ;
    }
    // input string ka number nikalne par
    int number = digit[index] - '0';
    // ye number kisse mapp hai vo nikalne par
    string mapp = mapping[number];
    for (int i = 0; i < mapp.length(); i++)
    {
    output.push_back(mapp[i]);
    printStr(digit, mapping, output, index + 1, ans);
    //backtrack
    output.pop_back();
    }
    }
    Is this efficent solution or Not ...

  • @yashgupta2675
    @yashgupta2675 2 ปีที่แล้ว

    O(digit)

  • @deepanshjohri3997
    @deepanshjohri3997 ปีที่แล้ว

    i solve this question because of you sir without any help
    void helper(vector &answers,string s,int pos,vector &phones,string answer)
    {
    if(pos==s.length())
    {
    answers.push_back(answer);
    return;
    }
    int number=s[pos]-'2';
    string str=phones[number];
    for(int i=0;i

  • @suruabhisekh
    @suruabhisekh 8 วันที่ผ่านมา

    string mapping[10]={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; best

  • @ujjawalpanchal
    @ujjawalpanchal ปีที่แล้ว

    python use: dictionary.

  • @dyno2337
    @dyno2337 6 หลายเดือนก่อน

    vector of vector

  • @Manishvr1
    @Manishvr1 ปีที่แล้ว

    20:52 tu bc

  • @sailendrachettri8521
    @sailendrachettri8521 2 ปีที่แล้ว +1

    // LEETCODE empty test case
    if(digits.size() == 0)
    return {};