Word Ladder | Leetcode
Vložit
- čas přidán 22. 09. 2020
- 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
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".
Yea. I did that in graph and I have flagged a correction there.
@@techdose4u Cool.. keep it up!!
@@chiragmali6752 thanks :)
Hey Man, I got placed today at Western Digital Corporation. Thanks a lot for your videos. Keep going🔥🔥
Woww great 🎉 Congratulations 💥 Would love to hear from you in LinkedIn. Please ping there.
how did you apply for the company,it was on campus or off campus??
this is gold, especially for the ones who have questions on when should we use DFS and when we should use BFS
Hello sir, I got placed at LinkedIn, thank you for your videos and constant guidance!!!.
Great ❤️
Great explanation..Really enthralled by the way of explaining every bit
Thank you so very much.Best explanation for this problem.
your explanations are so good that i just listen to them and go ahead and solve the problem
Nice :)
legendary explanation! Good job buddy!
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)?
Simply Makkhaan(Butter). What an amazing explanation....... Hat's Off.
Thanks :)
What tools do you use to draw on the screen? @TechDose?
Thanks you very much, I only needed the graph hint to solve this
Nice :)
excellent dry run!
Thank you uncle ☺️❣️
Your explanation is to the point. Keep it up!
Thanks
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?
Superb Thank you so much
Awesome explanation, thanks you so much :)
Welcome :)
Hello Tech Dose,
Amazing and detailed explanation.
What will be the space complexity?
Very Nice Explanation! Thank you.
Welcome 🤗
Very well explained.
Greatt explanation sir
Well explained, thanks
you really draw out all the words such an eyesoar man
😅
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
👍
You explained in an Excellent way
Thanks
Fabulous! Very clear
Thanks 😊
Well explained, but if you use set(for find()) then I feel complexity will be O(N2.WLogW).
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.
Yep. You already solved it :D
why using set gives better complexity?
@@ayushthakur2896 Constant time access to elements in Sets vs Lists I think?
Thanks, it was amazing.
Welcome :)
basically, we're assuming all edges are weight 1 and doing a djikstra.
in simple words BFS
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.
Can't we use dfs & dp to reduce complexity?
could anybody please tell me that why DFS does not work in this problem? Need help thank you~
Sirji pranaam 🙏🏻
🙏
Just Curious, why you disliked this ques? at 0:08 :P btw I always find your videos easy to understand. Thanks :)
😅
Great content 🔥🔥
Thanks
Thank you sir.
Welcome :)
this question was asked to me at amazon..
Nice :)
Nice explanation 👍
Thanks
U are great man
Thanks
Sir in graph we used visited matrix then why not here. The remove method here will charge O(n) thereby increasing the time complexity
Pls, discuss the 2-way Bfs approach also for this qs.
Sure I will try
amazing viedoe
Thanks
What's lsize doing? Please someone explain
Nice explanation
Thanks
Hey, well explained. Can you do it for word ladder 2 problem also ?
Already done.
Thank you sir.very good explanation .please good the same one in graph too sir .it is very useful to know multiple approaches
Welcome :)
hey bro apka brute force approch mind bloing he but kya iska optimal solution nhi he ?
so the main idea was to use O(n^2) to make the vali edges between words and then simply apply BFS?
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?
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).
@@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)?
@@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
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
Nice
Thanks
Please make a video on the Manacher's Algorithm..
Sure bro. But Currently I am covering graph :)
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
Sure
I liked your planning videos very much. Can you make a video telling us about the plan for those who are working in corporate
What is the space complexity?
I wish you are my csc sir
🤗
thank you so much..awesome..I am following your videos since 1 yr. I can't find your coding profiles anywhere.(want to see)😁
Thanks :)
are we using 0(zero) as true here?
What will be the complixity of this code?
❤
Why didn't you consider find and erase function time complexity in the Final Time complexity??
Btw great explanation
Maybe I missed 😅 It's easy to miss something when calculating time complexity.
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?
we have to start counting from first node
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...
how you can make dog from lot? this connection is invalid.
👍
👍
🙏
🙏
Design Search Autocomplete System can you make an video on this ?
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
i was able to do this using BFS bidrectional
Great ❤️
*** 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
😀
😁
code link is broken, can you fix it.
I will check
I tried using dfs+memorization but getting 44th testcase wrong anyone else
Is this code is in java or c++ ?
Cpp
I looking for bidirectional bfs approach in the video :(
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 :)
@@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.
@@yeswanthbonagiri7142 Yea 😅 I needed a proper asked question.
@@techdose4u leetcode.com/problems/open-the-lock/
leetcode.com/problems/jump-game-iv/solution/
Why used set??????????
I think you don't need to do line 14 to 21 in the latest version of the question
:o Dint check it
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
👍
Anyone noticed that TechDose has downvoted this question... 😂
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.
That was incorrect. I had flagged the correction when I was showing the graph. Dint realize it earlier in the previous slide.
uhh u can use bfs...
You mentioned curr.compare(temp) is taking O(N) time complexity, why not just check `if curr==temp` this would be O(1)
curr==temp is not O(1). its o(len of the string). its does a deep check.
Leave everything else, At 0:17 you also disliked the problem 😂
Github not found
It should work now. Try it.
Dond dell me whad do do
Thanks for the explanation.Just that too many ads , as I don't have premium.It is quite distracting.
This is not the optimal way to solve this question. An nlogn solution exists
could u please send that solution link?
Why there is so dislike on the question, even you also disliked it?
Which question ?
your code is not compact enough.
# 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
👍🏼
Great explanation..Really enthralled by the way of explaining every bit
Great explanation..Really enthralled by the way of explaining every bit
Great explanation..Really enthralled by the way of explaining every bit
Great explanation..Really enthralled by the way of explaining every bit
Great explanation..Really enthralled by the way of explaining every bit
Great explanation..Really enthralled by the way of explaining every bit
Great explanation..Really enthralled by the way of explaining every bit
Great explanation..Really enthralled by the way of explaining every bit
Great explanation..Really enthralled by the way of explaining every bit
Great explanation..Really enthralled by the way of explaining every bit
Great explanation..Really enthralled by the way of explaining every bit