LeetCode Letter Combinations of a Phone Number Solution Explained - Java

แชร์
ฝัง
  • เผยแพร่เมื่อ 31 ธ.ค. 2024

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

  • @crisag.2698
    @crisag.2698 4 ปีที่แล้ว +16

    Awesome video thank you Nick! To anyone who was a little confused at the while loop condition "while (output_array.peek().length() == i)", another way to do this is to capture the size of the queue at the moment in time in a variable, and then open up another for loop. So instead of doing this "while (output_array.peek().length() == i)" you could do this --> int size = output_array.size();
    for (int j = 0; j < size; j++) { // rest of code goes here }

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

      it's a bit slower(according to Leetcode)

  • @someoneanonymous8862
    @someoneanonymous8862 3 ปีที่แล้ว +12

    Can you slow down a Lil and explain the solution before you type code?

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

    for anyone wondering linked list is better use than arraylist here cuz we need to manipulate the data also ( linked list can act as a queue as well as a list) whereas arraylist only act as a list.

  • @sophiezhang6516
    @sophiezhang6516 4 ปีที่แล้ว +6

    i don't understand why we are using a linkedlist, not a arraylist or just a queue directly?

    • @Xavierrex3
      @Xavierrex3 3 หลายเดือนก่อน

      LinkedList implements the Queue interface, it implements a number of different data structures in Java actually

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

    I also solved it using a queue. You are doing a really great job man, you can describe what you are thinking excellent.

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

    You are genius Nick. I was stuck on this problem using the usual backtracking method of choose-explore(recursive)-unchoose. But your method is awesome. Can you please explain when to use the normal backtracking method and when to avoid it.?

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

      this problem can be solved with backtracking.

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

    I dont understand for sytax C++ because it has different function. Can transale someone for C . THANKS...

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

    Solution : private static final String[] MAPPINGS = {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    public static List letterCombinations2(String digits) {
    List result = new ArrayList();
    if (digits.length() == 0) {
    return result;
    }
    helper(digits, 0, "", result);
    return result;
    }
    private static void helper(String digits, int index, String combinations, List result) {
    if (index == digits.length()) {
    result.add(combinations);
    return;
    }
    String letters = MAPPINGS[digits.charAt(index) - '0'];
    // toCharArray() -> converts the given string into a sequence of characters.
    for (char letter : letters.toCharArray()) {
    helper(digits, index + 1, combinations + letter, result);
    }
    }

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

    Thank you so much! This solution is so smart, and your explanation is clear.

  • @KumarKaran-AI
    @KumarKaran-AI 4 ปีที่แล้ว +4

    Thanks Nick, you are doing a great job.
    Your videos are very helpful for people like us.
    Thanks a lot Mate! Wish you all Success !!!

  • @sparrow2068
    @sparrow2068 4 ปีที่แล้ว +1

    Why don't we use a map instead of a String array 2:50

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

    Hey Nick ! You are my idol
    i really a big fan of your crazy logic
    keep going Dude!...

  • @freefortravel4317
    @freefortravel4317 4 ปีที่แล้ว +1

    Excellent videos @nick. Can you please please share a video on Combination Sum II and subset II. Get a little confused on why is increase the depth by 1 in these cases and not in cases of Combination Sum and subset?

  • @KaranNagi
    @KaranNagi 4 ปีที่แล้ว +1

    i was asked the same question in Amazon online coding with an engineer, i solved the question by generating all the permutations, however the engineer asked me to optimize the code, and I was not able to think of anything, don't know if i will clear it. If anyone knows how to optimize this solution let me know. Cheers!

    • @mliu7712
      @mliu7712 4 ปีที่แล้ว

      I think there is no way to optimize the time complexity. After all, you need to permutate every possibility and that's actually the time complexity here. I think the interviewer is looking for a "no" to this question.

    • @jacktrimboli
      @jacktrimboli 3 ปีที่แล้ว

      @@mliu7712 I mean in the video it shows that nick's solution was only faster than ~64% of submissions. Best way to find those solutions would be to look at the analytics of the question on YT and look at the fastest solution

    • @abelsimon5308
      @abelsimon5308 3 ปีที่แล้ว +1

      you've generated permutations, but you only need combinations, that is still exponential time, but a bit better then permutations.
      The reason you need only combinations is because every letter of a specific number can be at the same idx, so ad,ae,af are all valid, however, you do not need the permutation of these like ea, da, fa

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

    I think this is combination problem, not permutation problem. CMIIW.

  • @Rdharini
    @Rdharini 4 ปีที่แล้ว +1

    Nicely explained. The stdouts help us understand the solution better. Love all your videos.

  • @سهيلهأيوب-س1ه
    @سهيلهأيوب-س1ه 2 ปีที่แล้ว

    bro that was helping, It's a queue solution

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

    awesome solution !

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

    OutputArr.peek().length() how it will work at length =0?

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

      because he adds an empty string to the linked list, so whenever he checks the first time, variable "i" will be 0 and the length of the empty string is equal to 0, so they are equal

  • @zo62
    @zo62 3 ปีที่แล้ว

    Surprised there isn’t a slick technique available

  • @ShubhamSingh-vh1vw
    @ShubhamSingh-vh1vw 4 ปีที่แล้ว +2

    This is a very different and beautiful solution .

  • @Matthew-hh6ex
    @Matthew-hh6ex 3 ปีที่แล้ว +2

    Hello guys, please anyone knows a group for developers where you can belong and grow in experience?

  • @papaz.z853
    @papaz.z853 2 ปีที่แล้ว

    Great as always

  • @MySWESpace
    @MySWESpace 5 ปีที่แล้ว

    Nice..thanks for this. Please can you explain the time and space complexity

  • @grandstarchief3226
    @grandstarchief3226 4 ปีที่แล้ว +1

    while(letterGeneration.peek().length() == i) is soooo clever

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

      I had trouble understanding this. If anyone could give a more detailed explanation it would be appreciated.

  • @Abhishek-nm9tm
    @Abhishek-nm9tm 4 ปีที่แล้ว

    Please make video on sorted permutation rank problem. with and without repetition.

  • @palaktiwari9407
    @palaktiwari9407 4 ปีที่แล้ว

    can you solve subsets problem 46 from leetcode please ?

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

    thank you!

  • @kurtcobain2355
    @kurtcobain2355 4 ปีที่แล้ว

    what is the time/space complexity here?

  • @bala-OG
    @bala-OG 2 ปีที่แล้ว

    Great

  • @piyushmathpal4244
    @piyushmathpal4244 3 ปีที่แล้ว

    omg, nick didn't clap

  • @akshatjain6854
    @akshatjain6854 4 ปีที่แล้ว +1

    Bro can you please tell me if this solution is generating combinations of more than size 2
    Like If I have 3245 , then will this solution give me combinations of length 4?

  • @palaktiwari9407
    @palaktiwari9407 4 ปีที่แล้ว

    sorry its permutation-46.

  • @aamirjamal6833
    @aamirjamal6833 5 ปีที่แล้ว

    Aah.. nice.. thanks..