Word Ladder | Leetcode

This is a very famous and important problem in graph algorithm which is the word ladder problem and it is from leetcode #127.This problem is a tricky graph problem which can be solved using a set, queue and using BFS algorithm.DFS can't be used for this problem because that can have exponential number unique paths in a graph.We can use either DFS or BFS to find shortest path in Tree but we need to use BFS for finding shortest path in graph due to polynomial time of BFS.I have first explained the problem statement with all the constraints and then I have shown the observations and intuition for solving this problem.After this, I have shown the dry run using SET and BFS algorithm using queue.At the end of the video, I have also shown the code walk through for the solution.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:-
BFS: • Breadth first search |...
Why DFS can't be used: codeforces.com/blog/entry/16479
#graph #graphalgo #leetcode

Пікірлер: 174

  • @chiragmali6752
    @chiragmali67523 жыл бұрын

    Very well explained. but there is one issue in 2 min 12 sec the path of length 7 is wrong. you have changed two chars instead of 1, while transforming "lot" to "dog".

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Yea. I did that in graph and I have flagged a correction there.

  • @chiragmali6752

    @chiragmali6752

    3 жыл бұрын

    @@techdose4u Cool.. keep it up!!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    @@chiragmali6752 thanks :)

  • @shubhamkhurana7545
    @shubhamkhurana75453 жыл бұрын

    Hey Man, I got placed today at Western Digital Corporation. Thanks a lot for your videos. Keep going🔥🔥

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Woww great 🎉 Congratulations 💥 Would love to hear from you in LinkedIn. Please ping there.

  • @raviashwin1157

    @raviashwin1157

    3 жыл бұрын

    how did you apply for the company,it was on campus or off campus??

  • @leowu5058
    @leowu50582 жыл бұрын

    this is gold, especially for the ones who have questions on when should we use DFS and when we should use BFS

  • @user-us7rg4cd6p
    @user-us7rg4cd6p2 ай бұрын

    legendary explanation! Good job buddy!

  • @kms8320
    @kms83203 жыл бұрын

    Hello sir, I got placed at LinkedIn, thank you for your videos and constant guidance!!!.

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Great ❤️

  • @rishabhkumar8115
    @rishabhkumar81152 жыл бұрын

    Great explanation..Really enthralled by the way of explaining every bit

  • @shivarammuthukumaraswamy7164
    @shivarammuthukumaraswamy71643 жыл бұрын

    Thank you so very much.Best explanation for this problem.

  • @otifelix
    @otifelix3 жыл бұрын

    your explanations are so good that i just listen to them and go ahead and solve the problem

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Nice :)

  • @birajparikh3367
    @birajparikh33673 жыл бұрын

    Hello Tech Dose, Amazing and detailed explanation. What will be the space complexity?

  • @roshanraut3093
    @roshanraut30933 жыл бұрын

    Simply Makkhaan(Butter). What an amazing explanation....... Hat's Off.

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks :)

  • @user-sr1pk3yy8b
    @user-sr1pk3yy8b3 жыл бұрын

    Hi, thanks for the problem explanation. Question: why don't we put "wordList" to a Dictionary instead of Set so to make word comparison O(1)?

  • @upendraroul343
    @upendraroul3433 жыл бұрын

    Your explanation is to the point. Keep it up!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks

  • @sangeetagautam733
    @sangeetagautam733 Жыл бұрын

    Thank you uncle ☺️❣️

  • @alpishjain1317
    @alpishjain13173 жыл бұрын

    excellent dry run!

  • @fadi.casual3796
    @fadi.casual37963 жыл бұрын

    What tools do you use to draw on the screen? @TechDose?

  • @SuperWhatusername
    @SuperWhatusername2 жыл бұрын

    Superb Thank you so much

  • @vinayppandey4321
    @vinayppandey43212 жыл бұрын

    Greatt explanation sir

  • @daanishsarguru3044
    @daanishsarguru30443 жыл бұрын

    Thanks you very much, I only needed the graph hint to solve this

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Nice :)

  • @akhilesh59
    @akhilesh592 жыл бұрын

    Very Nice Explanation! Thank you.

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    Welcome 🤗

  • @indraneelsarkar3452
    @indraneelsarkar34523 жыл бұрын

    Awesome explanation, thanks you so much :)

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @utsavgupta746
    @utsavgupta7462 жыл бұрын

    If we check popped string with the end word at the starting only not in the for loop then tym complexity will we O(n*26*w)? Am i right?

  • @madanmohanpachouly6135
    @madanmohanpachouly61352 жыл бұрын

    Well explained, thanks

  • @surajmaity6194
    @surajmaity6194 Жыл бұрын

    Very well explained.

  • @allen724
    @allen7243 жыл бұрын

    Can someone please explain why DFS doesnt work? I tried implementing with DFS and using a HashMap to store computed results already. But I am getting the wrong answer, getting 41/49 on leetcode.

  • @theghostwhowalk
    @theghostwhowalk3 жыл бұрын

    Like before video as I know this will be awesome 😀 BFS approach I guess? Where at each step check new word can be formed with char. Replacement from A to Z... For better time complexity, put all words in set.

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Yep. You already solved it :D

  • @ayushthakur2896

    @ayushthakur2896

    3 жыл бұрын

    why using set gives better complexity?

  • @LGL1999

    @LGL1999

    2 жыл бұрын

    @@ayushthakur2896 Constant time access to elements in Sets vs Lists I think?

  • @mikec9444
    @mikec94443 жыл бұрын

    you really draw out all the words such an eyesoar man

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    😅

  • @ramsaikoushikpolisetty6999
    @ramsaikoushikpolisetty69992 жыл бұрын

    Well explained, but if you use set(for find()) then I feel complexity will be O(N2.WLogW).

  • @pleasesirmorevideos4684
    @pleasesirmorevideos46843 жыл бұрын

    so the main idea was to use O(n^2) to make the vali edges between words and then simply apply BFS?

  • @rvrishav7
    @rvrishav72 жыл бұрын

    Can't we use dfs & dp to reduce complexity?

  • @jayalakshmij7623
    @jayalakshmij76233 жыл бұрын

    You explained in an Excellent way

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks

  • @ayushikumar34
    @ayushikumar343 жыл бұрын

    Thanks, it was amazing.

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @rajatgupta-ld1wg
    @rajatgupta-ld1wg3 жыл бұрын

    Just Curious, why you disliked this ques? at 0:08 :P btw I always find your videos easy to understand. Thanks :)

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    😅

  • @CodeSuccessChronicle
    @CodeSuccessChronicle2 жыл бұрын

    Fabulous! Very clear

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    Thanks 😊

  • @hangto0401
    @hangto0401 Жыл бұрын

    could anybody please tell me that why DFS does not work in this problem? Need help thank you~

  • @satyanarayanagokavarapu3011
    @satyanarayanagokavarapu30113 жыл бұрын

    Design Search Autocomplete System can you make an video on this ?

  • @HimanshuKumar-xz5tk
    @HimanshuKumar-xz5tk3 жыл бұрын

    What is the space complexity?

  • @sharaddabhi493
    @sharaddabhi4932 жыл бұрын

    What will be the complixity of this code?

  • @siddharthsinghvi9972
    @siddharthsinghvi99723 жыл бұрын

    I think there will be an error we have to return 0 at the end as if the endword is also present but there is no path in the graph to end word so we will return 0 then

  • @abhilashpatel3036
    @abhilashpatel30362 жыл бұрын

    What's lsize doing? Please someone explain

  • @kathirraja4709
    @kathirraja47093 жыл бұрын

    Instead generating new string and find in in set we could compare two words def is_one_word_change(w1,w2): if len(w1)!=len(w2): return False wc = 0 for i in range(len(w1)): if w1[i]!=w2[i]: wc+=1 if wc>1: return False return wc==1 this will reduce the time

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    👍

  • @amitavamozumder73
    @amitavamozumder733 жыл бұрын

    basically, we're assuming all edges are weight 1 and doing a djikstra.

  • @prakharagarwal6237

    @prakharagarwal6237

    2 жыл бұрын

    in simple words BFS

  • @willturner3440
    @willturner34403 жыл бұрын

    Great content 🔥🔥

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks

  • @kirtanprajapati8464
    @kirtanprajapati84643 жыл бұрын

    hey bro apka brute force approch mind bloing he but kya iska optimal solution nhi he ?

  • @niharikagrover3523
    @niharikagrover35233 жыл бұрын

    Hey, well explained. Can you do it for word ladder 2 problem also ?

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Already done.

  • @nitishgoyal9910
    @nitishgoyal99103 жыл бұрын

    Pls, discuss the 2-way Bfs approach also for this qs.

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Sure I will try

  • @saunaknandi1814
    @saunaknandi18142 жыл бұрын

    Sir in graph we used visited matrix then why not here. The remove method here will charge O(n) thereby increasing the time complexity

  • @anilchaudhry804
    @anilchaudhry8043 жыл бұрын

    this question was asked to me at amazon..

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Nice :)

  • @BaljeetSingh-bx8yj
    @BaljeetSingh-bx8yj3 жыл бұрын

    Sirji pranaam 🙏🏻

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    🙏

  • @gauravagarwal8592
    @gauravagarwal85923 жыл бұрын

    amazing viedoe

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks

  • @Mandeepsingh-jo5cf
    @Mandeepsingh-jo5cf3 жыл бұрын

    Thank you sir.

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @azeezsayyad7281
    @azeezsayyad72813 жыл бұрын

    U are great man

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks

  • @barshapaul9461
    @barshapaul9461 Жыл бұрын

    are we using 0(zero) as true here?

  • @onkarbagal8268
    @onkarbagal82683 жыл бұрын

    Nice explanation 👍

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks

  • @nikhilchandna4851
    @nikhilchandna48513 жыл бұрын

    Why didn't you consider find and erase function time complexity in the Final Time complexity?? Btw great explanation

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Maybe I missed 😅 It's easy to miss something when calculating time complexity.

  • @abhireddy8164
    @abhireddy81643 жыл бұрын

    Thank you sir.very good explanation .please good the same one in graph too sir .it is very useful to know multiple approaches

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @raghugore3990
    @raghugore39903 жыл бұрын

    Can u please provide solution and explanation for the below : Write a program to find Largest 4 digits Prime Number whose Sum of Digits is also Prime. 1.Prime Number is any number that is divisible only by 1 and itself. Examples are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31,........ 2.Sum of Digits means sum of all the digits in that number. Example is number is 1234 then the sum of digits is 1+2+3+4=10

  • @007-yashchoudhery4
    @007-yashchoudhery42 жыл бұрын

    how you can make dog from lot? this connection is invalid.

  • @vickysingh1320
    @vickysingh13203 жыл бұрын

    Please make a video on the Manacher's Algorithm..

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Sure bro. But Currently I am covering graph :)

  • @parteeksatija5536
    @parteeksatija55363 жыл бұрын

    sir for checking string we are using set which gives us the result in average O(1) time then why have you written there that for string compare it is O(N*26*N) it should'nt be O(N*26) at 15:39?

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    26 permutation for each letter. For a word, you will have 26*N. You can have N words at a level. So a total of (26*N*N).

  • @parteeksatija5536

    @parteeksatija5536

    3 жыл бұрын

    @@techdose4u Sir lets say there is a word "hit" now to try every combination time will be O(26*N) for one word and we can have total N words so time complexity would be O(26*N*N). Sir then What is W for in O(26*N*N*W)?

  • @paraschawla3757

    @paraschawla3757

    3 жыл бұрын

    @@parteeksatija5536 * TC O(26*N*N*W) * N - Number of characters in String * 26*N - Number of transformations happening in a string of N characters * 26*N*N - Comparison with endWord string ( String comparison is O(N)) * W - Number of Words in the BFS call Second multiplication by N is because all transformations i.e. 26*N occurrences of strings are being compared by endWord String comparison is O(N) , check stackoverflow.com/questions/55330338/time-complexity-of-string-comparison/55330371 W - total number of words added in queue

  • @dhanashreegodase4445
    @dhanashreegodase44452 жыл бұрын

    thank you so much..awesome..I am following your videos since 1 yr. I can't find your coding profiles anywhere.(want to see)😁

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    Thanks :)

  • @namithas6546
    @namithas65463 жыл бұрын

    Hi, I just had a doubt regarding the following test case. beginWord = "a" endWord = "c" wordList = ["a","b","c"] can anyone clarify why its output is 2 and not 1?

  • @lokeshkoliparthi9268

    @lokeshkoliparthi9268

    3 жыл бұрын

    we have to start counting from first node

  • @devyeag3096

    @devyeag3096

    2 жыл бұрын

    First start "a" change to it a to z if it is present as a same then we ignored it( suppose after convert a to a then it is same then we ignore it then check for b to z and depth = 1) and check for other character and it is find then add it (convert a to c it is find then depth = 1+1) and returns it...

  • @HimanshuSingh-ob9qo
    @HimanshuSingh-ob9qo3 жыл бұрын

    Nice explanation

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks

  • @rohanseth1392
    @rohanseth13923 жыл бұрын

    I learn a lot from your videos. Can you guide us of how you manage to stay dedicated for coding while being working in corporate ? I am working in corporate, I too want to go too far in coding, but not able to due. Please guide us

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Sure

  • @rohanseth1392

    @rohanseth1392

    3 жыл бұрын

    I liked your planning videos very much. Can you make a video telling us about the plan for those who are working in corporate

  • @lambar0
    @lambar0 Жыл бұрын

    Nice

  • @techdose4u

    @techdose4u

    Жыл бұрын

    Thanks

  • @swatygupta3753
    @swatygupta37533 жыл бұрын

    code link is broken, can you fix it.

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    I will check

  • @coder1015
    @coder10153 жыл бұрын

    I wish you are my csc sir

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    🤗

  • @TomJerry-bp9ig
    @TomJerry-bp9ig Жыл бұрын

  • @yeswanthbonagiri7142
    @yeswanthbonagiri71423 жыл бұрын

    I looking for bidirectional bfs approach in the video :(

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Don't worry. I will cover biderectional BFS in other questions. I thought it will be easier for most people to understand simple BFS. It will be helpful if you can recommend a most asked question in interview regarding birectional BFS :)

  • @yeswanthbonagiri7142

    @yeswanthbonagiri7142

    3 жыл бұрын

    @@techdose4u I think a simple question is : Given an undirected uniform graph, a source node and destination node, find minimum no of nodes exists in the path btw them. This is similar to word ladder 😅 I guess.

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    @@yeswanthbonagiri7142 Yea 😅 I needed a proper asked question.

  • @yeswanthbonagiri7142

    @yeswanthbonagiri7142

    3 жыл бұрын

    @@techdose4u leetcode.com/problems/open-the-lock/ leetcode.com/problems/jump-game-iv/solution/

  • @SHASHIKUMAR-pp4hg
    @SHASHIKUMAR-pp4hg3 жыл бұрын

    I tried using dfs+memorization but getting 44th testcase wrong anyone else

  • @ravishasharma1219
    @ravishasharma12193 жыл бұрын

    I think you don't need to do line 14 to 21 in the latest version of the question

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    :o Dint check it

  • @Dan-tg3gs
    @Dan-tg3gs3 жыл бұрын

    i was able to do this using BFS bidrectional

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Great ❤️

  • @sakshisinha8164
    @sakshisinha81643 жыл бұрын

    Why used set??????????

  • @arpanjena4595
    @arpanjena45953 жыл бұрын

    Is this code is in java or c++ ?

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Cpp

  • @HimanshuSingh-ob9qo
    @HimanshuSingh-ob9qo3 жыл бұрын

    👍

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    👍

  • @yatinarora1252
    @yatinarora12523 жыл бұрын

    I have tried this similar question from leetcode today...but the expected answer is 5 mine is 6.I was doing this some different algo..Bu I donot know that graph will be used in this question..Meaning this question is of graph

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    👍

  • @HimanshuSingh-ob9qo
    @HimanshuSingh-ob9qo3 жыл бұрын

    🙏

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    🙏

  • @designandcraft
    @designandcraft3 жыл бұрын

    😀

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    😁

  • @palakmantry
    @palakmantry2 жыл бұрын

    You mentioned curr.compare(temp) is taking O(N) time complexity, why not just check `if curr==temp` this would be O(1)

  • @manokumar89

    @manokumar89

    2 жыл бұрын

    curr==temp is not O(1). its o(len of the string). its does a deep check.

  • @anushkaagrawal1185
    @anushkaagrawal11853 жыл бұрын

    I have a doubt. in the given example how u transformed lot to dog as it is given that only one letter transformation is valid.

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    That was incorrect. I had flagged the correction when I was showing the graph. Dint realize it earlier in the previous slide.

  • @rahulkhatoliya6814
    @rahulkhatoliya6814 Жыл бұрын

    *** JAVA VERSION OF YOUR ALGORITHM *** class Solution { public int ladderLength(String beginWord, String endWord, List wordList) { boolean isEndPresent=false; Set St=new HashSet(); for(String s: wordList){ if(s.equals(endWord)){ isEndPresent=true; } St.add(s); } if(!isEndPresent) return 0; int depth=0; Queue Q=new LinkedList(); Q.offer(beginWord); while(!Q.isEmpty()){ depth+=1; int lSize=Q.size(); while(lSize-->0){ String curr=Q.poll(); int len=curr.length(); for(int i=0;i

  • @21RandomMan
    @21RandomMan2 жыл бұрын

    uhh u can use bfs...

  • @deathtrick
    @deathtrick3 жыл бұрын

    Github not found

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    It should work now. Try it.

  • @manasvinsharma1740
    @manasvinsharma17402 жыл бұрын

    Anyone noticed that TechDose has downvoted this question... 😂

  • @Sandeep-zd6dq
    @Sandeep-zd6dq Жыл бұрын

    Leave everything else, At 0:17 you also disliked the problem 😂

  • @jatinvishwakarma5272
    @jatinvishwakarma52723 жыл бұрын

    Why there is so dislike on the question, even you also disliked it?

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Which question ?

  • @SHASHANKRUSTAGII
    @SHASHANKRUSTAGII2 жыл бұрын

    This is not the optimal way to solve this question. An nlogn solution exists

  • @coder5518

    @coder5518

    2 жыл бұрын

    could u please send that solution link?

  • @mtvmango5172
    @mtvmango51722 жыл бұрын

    Dond dell me whad do do

  • @worldofafrontenddeveloper9187
    @worldofafrontenddeveloper91873 жыл бұрын

    Thanks for the explanation.Just that too many ads , as I don't have premium.It is quite distracting.

  • @jacobstech1777
    @jacobstech17773 жыл бұрын

    your code is not compact enough.

  • @jacobstech1777

    @jacobstech1777

    3 жыл бұрын

    # compact solution wordList = set(wordList) queue = collections.deque([[beginWord, 1]]) while queue: word, length = queue.popleft() if word == endWord: return length for i in range(len(word)): for c in 'abcdefghijklmnopqrstuvwxyz': next_word = word[:i] + c + word[i+1:] if next_word in wordList: wordList.remove(next_word) queue.append([next_word, length + 1]) return 0

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    👍🏼

  • @rishabhkumar8115
    @rishabhkumar81152 жыл бұрын

    Great explanation..Really enthralled by the way of explaining every bit

  • @rishabhkumar8115
    @rishabhkumar81152 жыл бұрын

    Great explanation..Really enthralled by the way of explaining every bit

  • @rishabhkumar8115
    @rishabhkumar81152 жыл бұрын

    Great explanation..Really enthralled by the way of explaining every bit

  • @rishabhkumar8115
    @rishabhkumar81152 жыл бұрын

    Great explanation..Really enthralled by the way of explaining every bit

  • @rishabhkumar8115
    @rishabhkumar81152 жыл бұрын

    Great explanation..Really enthralled by the way of explaining every bit

  • @rishabhkumar8115
    @rishabhkumar81152 жыл бұрын

    Great explanation..Really enthralled by the way of explaining every bit

  • @rishabhkumar8115
    @rishabhkumar81152 жыл бұрын

    Great explanation..Really enthralled by the way of explaining every bit

  • @rishabhkumar8115
    @rishabhkumar81152 жыл бұрын

    Great explanation..Really enthralled by the way of explaining every bit

  • @rishabhkumar8115
    @rishabhkumar81152 жыл бұрын

    Great explanation..Really enthralled by the way of explaining every bit

  • @rishabhkumar8115
    @rishabhkumar81152 жыл бұрын

    Great explanation..Really enthralled by the way of explaining every bit

  • @rishabhkumar8115
    @rishabhkumar81152 жыл бұрын

    Great explanation..Really enthralled by the way of explaining every bit