- 2 days ago
Transcript
00:00Next question, which we are going to talk about is longest word with all prefixes. I will give you a
00:05lot of words in an array. I can say that I will have a dictionary of words. Now, I have
00:10to take out of this whole word with all prefixes, which exist in all prefixes in these words. That means,
00:21that word also should be in this dictionary. And all prefixes,
00:27all the prefixes, they should also be in this dictionary. I have to find out the longest word. For example,
00:35if I take this dictionary example, I have a, banana, app, l, ap, apply and apple. In this way, I
00:44have many words. For this particular question, I have two possible answers.
00:51I can write an answer or apply. If my answer is apple, then how did the answer come from apple?
00:58What are the prefixes? One will be a, one will be a, one will be a, one will be a,
01:04one will be a, one will be a, one will be a, apply will be a, apply will be a,
01:10apply will be a, a, p, a, p, a, p, l, and a, p, l, y.
01:16Now, this dictionary exists in this dictionary, in this dictionary, in this dictionary, in this dictionary, in this dictionary, in
01:21this dictionary, in this dictionary, in this dictionary, in this dictionary, in this dictionary, in this dictionary, in this dictionary,
01:22in this dictionary, in this dictionary, in this dictionary, in this dictionary, in this dictionary, in this dictionary, in this
01:24dictionary, in this dictionary, in this dictionary, in this dictionary,
01:38So, in the dictionary, this word is also and it has all the prefixes. So, there are two possible answers,
01:43but in this case, we will print a small answer. So, alphabetically, which will be smaller. Smaller, this is a
01:52very important term.
01:56Smaller, lexicographically or alphabetically. A value is small as compared to C. C is big because A is small because
02:03it is small. A is small because it is first. A, P, P, L. These are both common. But E,
02:07Y is first. That's why I have to print in answer to Apple. Not apply. But if in my words,
02:14Apple doesn't happen. In that case, we can print apply as my answer. Now, three cases can be made.
02:23The first case will be in which my string exists, and the longest string exists, and for my string I
02:31have a single answer. The second case will be in which strings exist, which means multiple answers, so we will
02:39use lexicographically smaller print.
02:44And the third case will be where the answer doesn't exist. If the answer doesn't exist, then what will we
02:50do in that case? We will print empty. Which case will be where the answer doesn't exist? I have a
02:56word dictionary where I have strings. One string has AB, one string has AC, one string has AD. Now for
03:05this, what will I have? The answer will be null because no answer doesn't exist. Because if I take any
03:11string, let me take AB.
03:13If a string has a prefix, then I will have a prefix and a prefix. Now this prefix exists in
03:19the whole dictionary. If a string has a prefix, then I will have a prefix and a prefix, then it
03:25will not exist. If a string has a prefix, then I will have a prefix and a prefix, then it
03:30will not exist. So this means for this whole dictionary, I will have no valid answer. So there are three
03:36possible cases. Now I have to find out that I have all the strings.
03:45Now, what will we do in this question? We will think logically. Logically, we will do this first. What do
03:53I have to do? I have to make a word in my dictionary.
03:58plus all the prefixes also in my dictionary. Now, when we have a prefix in our mind, or when we
04:05have a prefix in coding, then in our mind, we need to think about tries. Because tries is a very
04:12important structure when we talk about prefixes. Now, if I talk about try, what is try data structure?
04:25Now, what do I need to do? I need to remove a word and all the prefixes already exist in
04:36dictionary. So why don't I need to create a try from all the dictionaries.
04:42If I create a try, then I have to work with prefixes. If I create a try, then I have
04:47to work with prefixes. If I create a try with these words, then I have to insert a null value.
04:54First we will insert a, then the end of the word will be true. Now I will insert a banana,
05:01then the end of the word will be true, then a, then a, then a, then a, then a, then
05:06a, and
05:07A, here the end of the word value will be true. Then I will insert the app, A is already,
05:13P will be me and another P will be true. Then I will insert the A, P, P and L,
05:22here the end of the word value will be true. Then I will insert the A, P and P, here
05:24the end of the word value will be true. Then I will insert the A, P and P, here the
05:27end of the word value will be true. Then I will insert the apply, A, P and L, here I
05:32will insert the Y, here the end of the word value will be true.
05:36Now we will insert a,p,l here we will insert e and end of the word value will come
05:43true. So I have this whole try structure which will create from this whole dictionary. Now we will notice a
05:50special thing here.
05:52The special thing will be noticed that in this try there are many characters that have the value of the
05:58end of the word. Usually, when we have asked the question of tries, there are so many words in which
06:05the value of the end of the word is true. There are some exceptional cases, but there are many characters
06:10in which the value of the end of the word is true. Now, we can remove a pattern here.
06:15Pattern is here that if my answer is Apple, then if I look at Apple's A character, then A is
06:22the end of the word true, P is the end of the word, P is the end of the word,
06:23P is the end of the word, P is the end of the word, P is the end of the
06:23word, P is the end of the word, P is the end of the word, P is the end of
06:23the word, P is the end of the word true, P is the end of the word, P is the
06:23end of the word true, P is the end of the word, P is the end of the word true,
06:23P is the end of the word, P is the end of the word, P is the end of the
06:25word, P is the end of the word, P is the end of the word, P is the end of
06:25the word, P is the end of the word, P is the end of the word, P is the end
06:26of the word, P is the end of the word, P is the end of the
06:32the end of the word value is true. So how do we solve this question from trice? There are two
06:39parts of the question. The first part is that the word should be in my dictionary. So I can say
06:44that trice in all of the words are stored in this dictionary already exist. So first I will get all
06:51the words that already exist in the dictionary. The second part is that the word
06:58all the prefixes should be stored in my dictionary. And what is the work of try? If you have many
07:06words, store all the unique prefixes in my dictionary. So I get to know that all the prefixes will be
07:15stored in my dictionary. Now what can I say for words?
07:19If I have any word, like apple, I will remove all the possible prefixes of this, apple, apple, apple, apple,
07:31apple, apple, apple, apple, apple, apple, apple, apple, apple, apple, apple, apple, apple, apple, apple, apple.
07:48So it will be so big. If I have a word which has all the prefix in the dictionary, then
07:56what can I say? That in this word, each letter will be true. Why will this happen? If I have
08:04taken an example of apple, then apple will be one possible word.
08:09If a is a, then the end of the word is true. The second possible prefix is a and p.
08:15If a is a and p, then the end of the word is true or the end of the word
08:20is true. The third possible prefix is a, p and p. The last p is the end of the word
08:27is true.
08:28in arrows 8. The fourth prefix will be A, double P, L. If A, double P, L will be true.
08:37And the fifth prefix will be A, double P, L, E. If A, double P, L, E will be true.
08:45If I see indirectly,
08:48So these are possible prefix in any word. Now if I want to store these prefixes in a try. How
08:55will it be stored? It will be stored in a single line. What does try do? Unique prefixes store it.
09:01If you store it in a 5, then it will store it in a single line. If you store it
09:05in a 5, then it will store it in a single line. If you store it in a 5, then
09:07it will store it in a single line. If you store it in a 5, then it will store it
09:11in a single line. If you store it in a 5, then it will store it in a line.
09:17one prefix which is only B, here two prefix which is B and A, here three prefix which is B
09:24and A, here four prefix which is B and A, here four prefix which is B, A, and A, so
09:27in this way all the prefixes will be made in each node, so Apple's all prefixes will be made in
09:33this line, apply's all prefixes will be made in this line, now if Apple's all prefixes exist in the dictionary
09:39then Apple's all prefixes
09:43unique words also need to be used in the dictionary which will exist in your own words and words will
09:50always be true in their last letter. Now we have a little bit of understanding, so we will pause it
09:55a little 15-20 seconds and rewind it again.
09:59One time you have to listen to this same thing, because when you listen to it, the picture will be
10:04clear. If all the prefixes exist in this word, then it means that they are individual words, so it means
10:10that the last character of the end of the word will be true. And if all the characters exist, it
10:16means that it means that every letter of the word will be true. So can I remove indirect logic from
10:23that? The logic came that I have to find out that I have to find out the longest word.
10:29from our try. And that word should happen in which the end of the word value should be true. So
10:39I have this logic that I have this logic that my longest word with all prefixes in the dictionary will
10:45be that word.
10:47which will be the longest word in my try, but with all the characters, the end of the word will
10:53be true. Like in apple, the end of the word will be true. Apply, the end of the word will
11:00be true. So, now I am going to remove this thing.
11:03I have to discuss my approach. What can I do? I can take a string. I will call it temporary
11:08answer. String temporary answer initialize with empty string. And what will I do? I will start increasing a level from
11:15above. For this root, I will see how many children are in which the end of the word value is
11:21true. If my answer is, then I need those children in the root, which is not null.
11:30a valid node value on top and the other, the end of the word value should be true. Then I
11:37can include that valid answer. So if I sit on root, I have two children. Both are not null. In
11:44one, the end of the word value is true and in one, the end of the word value is true.
11:47So obviously, I will get my answer to the true side. Here I will see how many children and one
11:54child is true. So here I will get my answer to the end of the world. If I talk about
11:59L, then I have two children. The end of the world is true. So in this case, I will get
12:04my answer to the end of the world. Now I will see the lexicographically little answer. So basically, I will
12:12go to each level and see which root node in the root.
12:17and end of the word value true. So I took a temporary string. What did I do? I started with
12:22the upper level. First I am on this level. I saw which children are not null and end of the
12:28word value true. I got a. What did I do? I added a temporary answer. This is my string temporary
12:35answer.
12:38Now I have reached the next level to A. Now A is my root. I can see children which is
12:43not null and end of the word is true. So this is P. So what did I say? P added
12:48in my answer. So in my temporary answer, I have stored P. Now 1 is temporary answer, 1 is final
12:55answer. 1 is temporary answer, 1 will be final answer. I will say final answer.
13:03In the final answer, like in the temporary answer, there is a string that is bigger from the final answer.
13:10So we will update the final answer. Now we will discuss the approach and then we will write the pseudocode.
13:14The final answer was null. Then the temporary answer was null. Then the final answer was null. The final answer
13:19was null. The final answer was null. The temporary answer was null. The temporary answer was null. The temporary answer
13:21was null. The final answer was null. The temporary answer was null. The temporary answer was null. The final answer
13:22was null. The temporary answer was null. The final answer was null. The final answer was null. The final answer
13:26was null.
13:33add to our temporary and when we compare the temporary value to the answer, we will also update the temporary
13:39value. Then we will go to the next level, P. In P children, there is a Consciacy node which is
13:44true and not null. For L, the end of the word is true
13:49So I added L in temporary and I updated the final answer. Now in children which value is not null
13:57and the end of the word is true. So first of all, I will get E because I am going
14:01to 0 from 26 to index. So obviously E will come first and Y will come later. So on E,
14:07I have seen E as the end of the word is true. So I have included E in temporary and
14:12I have included it in final answer. And in temporary and final answer, I just include it.
14:19that is greater than my final answer's length. Then I will update. In other cases, I will not update. So,
14:27here I have reached E. In E children, I have seen which node is not null and the end of
14:31the word is true. So, here all nodes are null.
14:34So, what will I do? As I went to L and then I went to E. Now I will start
14:39backtrack here. I will go back to E. Now I have reached L. Now I will reach L. Now I
14:45will see which children is not null, which is true. I will see E. Now I will see Y. And
14:52as I have backtracked E to L,
14:56e ko hattar bhi dungi final answer mein to e rahe ga kyunki abhi taq ka sabse lemba string hai
15:01,par mein yahaan se temporary se e ko hattar dungi kyunki ab mein e par nahi ho,ab mein l
15:05par ho,tho yahaan per last mein l ho na chahi hai,ab mein dhekhun ghi l ka dousra aysa
15:09kaunsa children hai,jiski value not nal hai end of the word true hai
15:14So I will add Y in temporary Y is added in temporary Y is added in temporary Y is added
15:18in temporary Y is added in temporary Y is added in temporary Y is added in temporary Y is added
15:20in temporary Y is added in temporary Y is added in temporary Y is added in temporary Y is added
15:24in temporary Y
15:29As I went to L to Y, I will see which children are here, all the children are null, so
15:35what do I have to do with that? We will go backtrack again to L. As I went backtrack again,
15:42we will remove Y from here. Now I am going to L, I will see all the children here, we
15:47will go backtrack again to P. As I went backtrack again, I will also remove L. Now I have seen
15:51all the children here, we will go backtrack again to P. As I went backtrack again, I will also remove
15:52L. Now I have seen all the children here, we will go backtrack again.
16:00Now we will backtrack here, backtracked then we will take the P.
16:07Now I have to go to A. Now I have to go back to A. Now I have to go
16:11back to A. Now I have to go back to A. Now I have to go back to A. Now
16:15I have to go back to A. Now I have to go back to A. Now I have to go
16:18back to A. Now I have to go back to A. Now I have to go back to A. Now
16:22I have to go back to A. Now I have to go back to A. Now I have to go
16:23back to A. Now I have to go back to A. Now I have to go back to A. Now
16:28I have to go back to A.
16:35Answer and the second string is temporary. Now what do I do? I have to traverse my tree. When we
16:43are going to level by and level by and then backtrack backtracking is a concept of recursion. So here we
16:50will make recursive function. How do we make recursive function? I will write recursive function which I will name longest
16:57word. I will keep return time void and we will name longest.
17:02In the longest word, I will have two parameters. One parameter will be on the root and the other parameter
17:09will be my temporary answer. If someone has created my try data structure then I am on which root node
17:16and what is the temporary value. Now, I will have a string outside. I will initialize it with empty string
17:25and this is basically my final answer. I store my final answer in the answer.
17:30Now if I am going to a root for all the children, I am going to use a loop for
17:35all the children. I am going to use a loop for all the children. I am going to use a
17:37loop for all the children. I am going to use a loop for all the children. I am going to
17:40use a loop for all the children. I am going to use a loop for all the children.
17:572 conditions should be true, 1 my child should not null and 1 end of the word value should be
18:02true, so where my root.child is not null and where the end of the word value is true, when
18:07these two conditions will satisfy. If both conditions will satisfy in any index, then what to do in that case?
18:13In that case, what do we do? First, we insert the character in temporary. First, add the character in temporary.
18:25Which character? Which is in the root. We will add that character. Then we will check the length of the
18:34temporary. If the temporary length is greater than the answer, then we will update the answer. The answer to update
18:44is that I have assigned my temporary value. Now, in the recursion, there are two things. If I have made
18:51a try, this is A, this is P.
18:54This is B. If I try structure, as I go down, what do I do? I add a character in
19:01temporary. Here there is a character in temporary. But when I go backtrack, this character will be removed. As we
19:12backtrack again, which character we have added in temporary, this character will be deleted from temporary. So we will delete
19:21this character temporarily.
19:23After updating the answer, we will call the longest word for the longest word. What will pass in the longest
19:31word? We will pass our root child node, which index is high, and it is temporary. So in this way,
19:40My for loop will go. What have I done? I put all the children to a loop. The children is
19:46not null and the end of the word is true. I added the character in temporary and then I added
19:50the answer to the answer and updated. Then I added the longest word, same recursive function to call for the
19:56child node. Now we will backtrack again. I added the value that I had added in temporary. We will delete
20:03that value.
20:06So, the last character we have added, we will delete that last character. So, all the parts will come into
20:16my for loop. And I don't need to keep the base case, because in the base case, root is equal
20:20to my root. We want to keep clarity, but we don't need to keep trice case. So, if the root
20:29is null to any point, we will simply save the return. So, what is the process?
20:36I will make an answer. I will make a static type. My final answer will store. I will make a
20:41recursive function with the longest word name. I will make a root and temporary name. If I can make a
20:47string or temporary, we can also make a string builder. If we make a string builder, we will be able
20:53to make a time complexity. String builder is optimized as compared to string.
20:58We learned in the class of string builder that when we add or delete a string every time. So, add
21:07and delete all of our work, we will implement a string builder temporarily.
21:13Now if the root is null, simply return or not, we will add a loop to the child nodes. We
21:18will see the child nodes which are desired, we will add a temporary character. Then we will update our answer
21:24temporarily, and we will add a recursive call for the next level, for the child node. And last, we will
21:31delete that character. So, we will write our java code.
21:36In our public class, I have already created a static class node, a root. I have written a function for
21:42insert. I have written a function for search. In the main function, I have already written all the words in
21:48the main function. First, we will create a try data structure from these words. So we will put a loop
21:53for int i equals zero, i less than my words dot length i plus plus. We will call insert for
22:02my words of
22:03So, all the words we will insert into try. Now, we will write a function for longest word. Public, Static,
22:12Void, Longest Word. In the longest word, I will have one node root. On that, I will have a string
22:20builder, which we will name as temporary. And a static, I will make it public static string answer type. So,
22:32we will initialize the answer.
22:34with empty string. Now in the longest word, my base case said that if the root value is null, what
22:40do we have to do in that case? Simply do return. This case will never hit, it's just for clarity,
22:47because we have implemented in this way that the root is null. But you can write it again.
22:53In case there is a corner case, you have the longest word in which you can tell that the words
22:59are completely empty. So in that implementation it can be possible that the root has never been initialized or null.
23:06So we have written it for that special case. Then I will run a loop for all the children. for
23:11int i equals 0 i less than 26 i++
23:17What do you want to do every time? First, check what my root of children at index i is not
23:23null value. If it is not null value, then what do you want to do? Let's see that the end
23:30of the word of children is true.
23:32If both conditions are satisfied, then we will do all the work. What do we do? First of all, we
23:38have to append in temporary. In string builder, there is a .append function. For any string builder, add your character.
23:45What is my character? My character is basically character ch. This is basically char i plus a.
23:55I know whatever my it index is my character's index, so if I add a to this index, then I
24:01have my whole character, but it is now in the integer, so I have typecast it in the character and
24:06store it in this ch, so I will do it in temporary.append ch and after that we will compare
24:11what temporary length will be greater than the answer. If it is, then update it and the answer will be.
24:20temporary dot to string equal. So string builder's value can't assign directly to string. That's why we use two strings
24:26method and assign temporary value to the answer. After that we will add temporary and update it. After that we
24:35will use recursive call. Longest word will call for my root.children at index i and my temporary. And the
24:42way we backtrack again, this is my backtrack step. As we backtrack again,
24:49Now we will delete character at index temporary.length-1 I have given the last character of index temporary.length
25:05-1 This is my last character of index and we will delete that character. So this whole code is complete
25:12for my longest word. Now we will print from here our longest word function.
25:19answer to pass. I have passed my try root and with this function in fact, this function will be different.
25:28We will calculate the longest word and in the start we will pass the new string builder which has been
25:36initialized with an empty string and in last we will print this static answer. Let's save and run.
25:49The answer is apple equal. In fact, if this string doesn't happen here, then the answer is apply equal.
25:56because it has given lexicographically smaller, so apply. Now you can ask here that we haven't sorted alphabetically anything. So
26:04how do we know that apply will come first or apple will come first? It will know that my loop
26:10is coming from 0 to 26. So from 0 to 26, then A will come first, then B will come,
26:15then C will come, then D will come. So if it will be the same length, then the lexicographically smaller
26:20combination of lexicographically or alphabetically, the smaller my string will be smaller.
26:26we have already covered it and if we get the same length later then we will not be updated because
26:30when we get the same length, we do not update for the same length. That's why we have ensured that
26:36the lexicographically smaller letter comes first. But if you ask me to ask me to ask me to ask me
26:43to ask me to ask me to ask me to ask me to ask me to ask me to ask
26:45me to ask me to ask me to ask me to ask me to ask me to ask me to
26:47ask me to ask me to ask me to ask me
26:53What do you do? Start this loop from 25 and in the loop i is greater than or equal to.
26:59You mean if you just leave the loop completely, then it will be larger. I just want to leave this
27:03loop, I have to leave this loop. Once we save and run, let's see what will be answered. This time
27:08the answer will be applied. So the answer will be applied because this time I have left the logic. This
27:14time it will not be alphabetically smaller, it will be alphabetically larger. So in this way we solve our longest
27:19word questions.