Word Ladder 2 | Leetcode

Sdílet
Vložit
  • čas přidán 27. 09. 2020
  • This video explains a very important graph interview problem which is word ladder 2 and it is from leetcode 126.It is a follow-up of the word ladder 1 problem which i have already explained and the link for that will be present below.In this problem we are required to find all possible paths from beginword to endword from a given wordlist.Each transformation must be of 1 character only.I have shown how to make a special adjacency list which has only child nodes as adjacent nodes.Using this adjacency list, we can simply apply DFS to find all possible shortest transition paths from beginword to endword.An alternate solution can be to first find the depth of shortest transition using BFS and then apply DFS upto only the minimum depth value.CODE LINK is present below as usual. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
    ========================================================================
    Join this channel to get access to perks:
    / @techdose4u
    INSTAGRAM : / surya.pratap.k
    SUPPORT OUR WORK: / techdose
    LinkedIn: / surya-pratap-kahar-47b...
    WEBSITE: techdose.co.in/
    TELEGRAM Channel LINK: t.me/codewithTECHDOSE
    TELEGRAM Group LINK: t.me/joinchat/SRVOIxWR4sRIVv5...
    =======================================================================
    CODE LINK: gist.github.com/SuryaPratapK/...
    USEFUL LINKS:-
    Word Ladder 1: • Word Ladder | Leetcode...
    BFS: • Breadth first search |...
    DFS: • Depth first search | D...
    #graph #graphalgo #wordladder

Komentáře • 92

  • @RahulVerma-fz2jf
    @RahulVerma-fz2jf Před 3 lety +27

    Really good. After going to 10 editorials, I finally understood Word Ladder 2 using this video. I am sure, I won't be forgetting it next time. Keep up the good work. :)

  • @JCatharsis
    @JCatharsis Před 2 lety +3

    Your approach seems the most intuitive and easy to understand compared to all leetcode solutions.. cheers mate

  • @madmax2442
    @madmax2442 Před 3 lety +5

    This was a tough one and still you made so clear! Hats off 🎩

  • @shivanshrawat2688
    @shivanshrawat2688 Před 3 lety +3

    I was just going to post a comment to ask you to put out a video for this problem. there is simply no good explanation for this one, and I was constantly checking your page if you have uploaded solution to word ladder 2 because i knew that i would only understand it over here. Many thanks and more thanks. This page is absolutely brilliant.

  • @babulbhanu8213
    @babulbhanu8213 Před 3 lety +17

    Teaching is an art and you have mastered it . Thank you so much :)

  • @avinandanbanerjee9568
    @avinandanbanerjee9568 Před 3 lety +6

    Thanks for the clear explanation! I wasn't able to get the special adjacency list, and leetcode solutions were very cryptic.

  • @sthirumalai
    @sthirumalai Před 3 lety +5

    Hi, Your thought process is crystal clear. I implemented the solution using two BFS iterations instead of a BFS+DFS. But the intuition is the same. Great content. Thanks

  • @shivambharti8278
    @shivambharti8278 Před rokem +13

    Giving TLE as of now. 32/35 test case passed :)

  • @code_life4835
    @code_life4835 Před 3 lety +1

    Great Explanation Sir!
    Congrats for 30K Subscribers!

  • @aushoroup2992
    @aushoroup2992 Před 3 lety

    "Special Adjacency List" is the key. I got TLE while using the Normal Adjacency list. Thanks for helping me out.

  • @1tav0
    @1tav0 Před 2 lety

    Thank you so much for this explanantion this problem is not easy at all and ive learned it with this video. thank you so much

  • @yitingg7942
    @yitingg7942 Před 3 lety +2

    lol at the first second when I saw the graph you drew, I knew how to solve it. Thank you for being so inspiring Sir!

    • @techdose4u
      @techdose4u  Před 3 lety +1

      😂 shit. You dint watch the video 🤣

    • @yitingg7942
      @yitingg7942 Před 3 lety +1

      @@techdose4u hahahaha you caught me Sir!
      I actually still did Sir. I am half way now, trying to write the code while watching the video.

    • @techdose4u
      @techdose4u  Před 3 lety

      Nice 😊 All the best 👍🏼

    • @yitingg7942
      @yitingg7942 Před 3 lety +1

      @@techdose4u Sir I was so naive, I thought I knew the answer so I skipped the second part. Then it took me hours struggling without getting the code right. Today I came back and saw the second part, only to find that I am a complete idiot by skipping it. Since you made everything crystal clear. If I have followed your process, I wouldn't struggle for that long.
      Thank you so much Sir. I appreciate everyday that you are here!!!!!!!

    • @techdose4u
      @techdose4u  Před 3 lety +1

      Atleast you tried on your own. I am pleased to see that ❣️

  • @rosail21
    @rosail21 Před 3 lety +1

    Excellent Solution....Love your detailed analysis....Very unique an Enjoyable!

  • @MrJoyRana
    @MrJoyRana Před 2 měsíci

    Great man!! I really struggled to understand and found this one, I was assured it's techdose :)

  • @andreafabris893
    @andreafabris893 Před 3 lety +6

    Could you explain why the time Complexity of DFS is V*E in this case and not V+E? Thank you

  • @_inspireverse___
    @_inspireverse___ Před rokem +1

    Awesome! You made it so easy and clear ❤❤

  • @learn5371
    @learn5371 Před 2 lety

    People like you make India proud

  • @abhishekbarman1837
    @abhishekbarman1837 Před 3 lety +3

    I tried the code but instead of "dict.count" i used "dict.find" it gave TLE can u explain why ? and same for visited

  • @lisaren6661
    @lisaren6661 Před 2 lety +1

    Wow! This is super clear. Thanks you so much!

  • @nikhilpawariaaa
    @nikhilpawariaaa Před rokem

    THIS HELPED ME A LOT! THANKS

  • @sushantkumar9711
    @sushantkumar9711 Před 2 lety

    I think if we are facing difficulty understanding the special adjacency matrix. we can create a normal adjacency matrix and check if the current level is equal to the min level which we get from the BFS part and the current string is equal to the end word. if this is the case add else not. any views on this?

  • @KM_1947
    @KM_1947 Před 3 lety

    Really good explanation. Can you clarify why BFS alone would not suffice. While calculating minimum depth itself can't we maintain the path from startWord to the current node as an attribute of that node and return that attribuite when we reach endWord.

  • @vinayakgandhi5690
    @vinayakgandhi5690 Před 2 lety

    Instead of using the special adjacency list, can we just create a normal adjacency list like an undirected graph and keep a dfs visited array to make sure we don't visit any node again in one dfs path, but can visit it in some other dfs path?

  • @vishalmishra9549
    @vishalmishra9549 Před 2 lety +1

    Thank you for this great solution. Learned new thing.

  • @Phoenix-xi2gy
    @Phoenix-xi2gy Před 3 lety +1

    Can u pls explain how the time Complexity of DFS will be V*E in this case? BTW love your videos.. Keep doing the good work!

  • @harshitpandey7521
    @harshitpandey7521 Před 3 lety +5

    Why is the complexity of DFS traversal O(VE), shouldn't it be O(V+E)?

    • @tanujgupta143
      @tanujgupta143 Před 2 lety

      it is not a simple DFS call. Simple traversing nodes using DFS gives O(V+E). This DFS prints all possible paths. Hence its worst time can be O(VE), best case can be O(V+E)

  • @chris.w391
    @chris.w391 Před 3 lety +1

    18:16 DFS in adjacency list should take O(V+E), right?

  • @rahulchudasama9363
    @rahulchudasama9363 Před 3 lety +2

    Through this video, I realized what u think u can do it in code.
    Many times I feel whatever I am thinking as a solution is lengthy and it will be hard to debug and what if it will not work with hidden/many test cases?
    Thanks, TechDose :)

  • @tapeshmittal3287
    @tapeshmittal3287 Před 3 lety +1

    Super video! I applauded for $2.00 👏

  • @satyanarayanagokavarapu3011

    Can you explain Guess the word problem ? That would be helpful

  • @nikhilmehta7298
    @nikhilmehta7298 Před 2 lety

    It will return empty matrix after executing same code please help me in that

  • @secondIncomePartTime
    @secondIncomePartTime Před rokem

    bhad faad diya bhai

  • @PraddyumnShukla
    @PraddyumnShukla Před rokem +2

    This solution gives TLE

  • @shoaibakhtarshaikh3840

    sir we should be able to traverse from both the sides ?what if we want to transform word log to hit in this case we will not be able to find the path

    • @benjaminross5615
      @benjaminross5615 Před 3 lety

      The structure of the DAG that is generated using this modified bfs algorithm is dependent on the start word and stop word. If the start word is “log” and the stop word is “hit” the graph would look different.

  • @dataman4503
    @dataman4503 Před 2 lety +2

    This problem is a bit hard. To solve this problem we need to use many DS (maps, queues, vectors,... ) . Lol.. I think if such questions are asked in Inteviews there is possibility that 1 hour will be consumed by this 1 problem alone.

  • @MrNeo086
    @MrNeo086 Před 3 lety +1

    Amazing!

  • @dhanashreegodase4445
    @dhanashreegodase4445 Před 2 lety +2

    gonna sleep for straight 2 hours after this question

  • @sauravawesome
    @sauravawesome Před 3 lety +1

    if I use vis[temp] == 0 instead of vis.count(temp)==0 ,it gives runtime error can't see how or why ,can you please make this clear

    • @madmax2442
      @madmax2442 Před 3 lety

      Because vis[temp] = 0 means that you have you have visited 'temp' and its level is zero (and in actuality, temp is still unvisited) and so you will not include it in your path.
      We have to include the ones who are not visited.. So, either use "vis.count(temp) == 0" or "vis.find(temp) == vis.end()" to apply the correct logic.

    • @shiqilyu6692
      @shiqilyu6692 Před 2 lety

      same problem.

  • @Donutshop2365
    @Donutshop2365 Před 3 lety +3

    Really good intuitive approach, but would TLE.

    • @techdose4u
      @techdose4u  Před 3 lety

      :O

    • @uditagrawal6603
      @uditagrawal6603 Před 3 lety

      @@techdose4u Initially was getting TLE but after optimisation with adjacency list it worked out.
      Nice approach!!!

  • @amitupadhyay6511
    @amitupadhyay6511 Před 2 lety +2

    It gives TLE in python

  • @LeoLeo-nx5gi
    @LeoLeo-nx5gi Před 3 lety +1

    Sir pls if u start DP pls do good hard level problems bcoz whether any one says or not one can find the easy level and most famous dp problems everywhere so that would really be waste of time to go on with the usual ones like LCS and so on.... I hope u understand Sir, sorry if I said something wrong but I wanted to say it before u were about to start🙏

    • @techdose4u
      @techdose4u  Před 3 lety +1

      I will do problems of different types. All questions can't be very hard else very few will understand. I will do mix of problems.

  • @PriyanshuKumar-ho7kj
    @PriyanshuKumar-ho7kj Před rokem

    Gives TLE now...although good approach

  • @RajeshKumar-ts4fw
    @RajeshKumar-ts4fw Před rokem +1

    this solution gives tle now

  • @gomzysharma
    @gomzysharma Před rokem +1

    Getting TLE with this code+approach. Anyone else?

  • @rupalimaheshwari3024
    @rupalimaheshwari3024 Před rokem

    26^N?

  • @anujtyagi4047
    @anujtyagi4047 Před 2 lety +2

    your code giving tle

  • @UTTAMKUMAR-wd4zz
    @UTTAMKUMAR-wd4zz Před rokem +1

    TLE aara bae.

  • @yogeshsikanderpuriya7417

    getting tle now

    • @juzarantri864
      @juzarantri864 Před rokem +1

      did u solved bro??

    • @player-te8tf
      @player-te8tf Před rokem +1

      @@juzarantri864 Yea

    • @juzarantri864
      @juzarantri864 Před rokem +1

      @@player-te8tf please tell me from where did u find the approach and how u did it?

    • @player-te8tf
      @player-te8tf Před rokem +1

      @@juzarantri864 man i mean i solved it with the approch explained in the video, but it gave TLE.
      and this s*it requires higher brain functionality which i lack now... So i have added it in my to do list and will look into this q after i complete graph

    • @player-te8tf
      @player-te8tf Před rokem +1

      And yea... If u really wanna solve it then go to the leetcode discussion >

  • @ghazanferwahab5673
    @ghazanferwahab5673 Před 2 lety

    plz give coe in java

  • @LegitGamer2345
    @LegitGamer2345 Před 3 lety +3

    java version :
    class Solution {
    public List findLadders(String beginWord, String endWord, List wordList) {
    HashMap adj = new HashMap();
    HashSet dict = new HashSet(wordList);
    Queue Q = new ArrayDeque();
    Q.add(beginWord);
    HashMap visited = new HashMap();
    visited.put(beginWord,0);
    while(Q.size()>0){
    String curr = Q.remove();
    for(int i =0;i

  • @CareerHunt
    @CareerHunt Před rokem +2

    //100% pass test cases
    class Solution {
    public:
    void findAllPath(vector &res, string currentWord, string beginWord, unordered_map &parents, vector path) {
    if(currentWord == beginWord) {
    path.push_back(beginWord);
    reverse(path.begin(), path.end());
    res.push_back(path);
    return;
    }
    for(auto word: parents[currentWord]) {
    path.push_back(currentWord);
    findAllPath(res, word, beginWord, parents, path);
    path.pop_back();
    }
    }

    vector findLadders(string beginWord, string endWord, vector& wordList) {
    unordered_map wordMap;
    unordered_map parents;
    unordered_map levels;
    for(auto word: wordList) {
    wordMap[word] = true;
    }
    queue q;
    q.push(beginWord);
    int level = 0;
    levels[beginWord] = 0;
    while(!q.empty()) {
    string s = q.front();
    q.pop();
    for(int i=0; i

    • @tausifnawaz8187
      @tausifnawaz8187 Před rokem +1

      can you please explain the problem in the original code and what changes you made to make the code work, I'm struggling to come to a conclusion???

    • @CareerHunt
      @CareerHunt Před rokem

      @@tausifnawaz8187 from this method, we are coming from end to starting while calculating path

    • @CareerHunt
      @CareerHunt Před rokem

      @@tausifnawaz8187 in the above video code, time limit happening due to multiple DFS on neighbours while calculating path.
      Suppose our level is more than 1+ level of curr. Then, We were not doing anything with those neighbours. But it should not be like that.
      So, in this approach we are trying to remove those neighbours whose level diff is more than one.

    • @nanotasza
      @nanotasza Před 3 měsíci

      @@CareerHuntThis is genius, thanks!