- 1 day ago
Transcript
00:00Next question which we are going to talk about is word break problem and this is a very famous problem
00:04which came in Google interviews.
00:07In the problem, I will give an input string and some dictionary words. I have to find out that the
00:13input string is my input string, can I make a space separated sequence of dictionary words? If I simplify this,
00:20I will give two things which will be input of my question. The first thing is my words name, which
00:26will have many words in the form of strings. The other thing that I have given in the question will
00:32be a key. Now, I have to try this key.
00:37so that all the words come from my original word array. If there is a word that doesn't have an
00:43original word, then I have to return false. Or if I use all these words, if I divide my string
00:50in small sequences or part, then I have to return true. Like if I use this key example, then I
00:58can create three parts. One will come from I.
01:04One will come like and one will come samsung. So, combine three words by combining my whole key string. And
01:10these three words exist in my word array. One, two and three. But if I have another key given here,
01:18like I have given, I, like,
01:21sunk. In this case, this is a single word that exists here. This is also a single word that exists
01:27here, but this word doesn't exist in my words. In this case, I have to return false. Now, for solving
01:36this problem, what approach we will use? Our approach that we will use is my substrings approach.
01:41I will divide it in every possible string and check whether that string exists within words or not. And since
01:51we are talking about tries, we have learned that if I have to search for any string that exists within
01:56words or not, then we use a try data structure because it gives us a good time complexity. So in
02:04an optimized way, I will have a solution. So what am I going to do? First of all, I will
02:10I will create a try from my strings. The second step which I will perform is that I will try
02:19to divide my key in every possible parts. So here the first part which I can do is that I
02:26will check the I in my words, my try exists in my words. And then like Samsung, the rest of
02:33the key will remain. For this key, I will do a recursive function.
02:38and I will find out that the rest of my words exists in this recursive function. I have a point
02:46in which I have divided into two or more parts of the key, where this part exists and there are
02:53multiple parts that exist in this case. In that case, I will return true that this part exists in my
02:59try and the key from the other. My try has returned true. But if it falls in any case, then
03:06I will return true.
03:07false. Now for my key, all possible substrings will come in which form. First, I will run a loop that
03:14will come from i is equal to 0 to my key's length. Then, what will I do? First, when i
03:19is equal to 0, I will do partition here. Partition here, I will check first
03:27If my try exists, I will perform a search operation and the rest of the sequence, which will be saved,
03:33I will put a recursive call again. If my key will be saved, I will put one more call to
03:40check if my try exists in any form. After that, I will know that I exist in my words area.
03:49So, I got a true return here.
03:51And what will be for the key? First of all, I will put a partition on this i is equal
03:57to 0. When the first partition starts, L will be different here and the rest of the key will be
04:03written in this way. So what will I do? First of all, I will search for L. When I start
04:10searching for L, I will know that L doesn't exist in my try.
04:15In fact, what do I do? We make try and make it a try. I have a root node in
04:22my try. First, I insert I here, then like I insert L I K E. After Sam insert it will
04:32be S A M. After Samsung insert it, S A M already. S U N G. After mobile insert it,
04:46so it will be M O B I L E and last I have to insert I C E so
04:56here I will insert C and here E and this will be my end of the word true this is
05:03also true this is also true and in Samsung M is also true
05:07and in mobile E is also true. So in this way, I have to create a try in the beginning.
05:12Then I will run a loop on my key and divide my key every time. I have to search directly
05:18in the first part what I have to do in my try. And I have already written the code of
05:22search. I will perform the search operation in the second part. And for the second part, I have to add
05:28a recursive call. I will pass it as my key to the next level. Then what will I do next
05:33level? I will partition it here.
05:45Now we are talking about i is equal to 1 Now we are talking about i is equal to 2
06:02Now we are talking about i is equal to 2
06:05And the rest of the key will be E, S, A, M, S, U, N, G. So, this L, I,
06:11K doesn't exist in my try. So, next partition will be if it doesn't exist. So, here will be L,
06:18I, K, E. And the last part will remain in me.
06:22This will be partition for i is equal to 3. How is partition? Basically, I calculate my substrings in the
06:28partition. I already know how the substrings come out of the substring. I will use the .substring function in which
06:34first pass my first index and then pass my last index. So, every time if i is equal to 0
06:42from length to length, every time my partition will be 0 from i. This will become my first string and
06:48the i will be the length of the key.
06:51Last key, rest of the key string. So when I will do this partition, i is equal to 3, I
06:57will see what like exists in my try. Here I will get LB, IB, KB and E where my end
07:05of the word is true. Like is an original word that exists in my string.
07:10So here it will be true for me. Then what will I do? I will put a recursive call again.
07:18When I put recursive call again, we will check again with samsung partitions. How will samsung partitions? I will write
07:26here samsung. In the first partition, I will just check for this partitions.
07:33I will check for mSung. So if I checked for S, I got here, but here the end of word
07:39is false. It is not true. So I will give me the search for false return. After the next partition,
07:45the next partition will be A. Now I will check for S, A, but here the end of word is
07:52false.
07:52So this will also be false return. Then after next partition M will also be false return. This will also
07:57be false return. In this way, I will partition it with a character, which is my last partition, which I
08:03will search for Samsung. In that case, I will have sb, a, b, m, b, s, u, n, g. And
08:10in last, my end of the word will also be true. In that case, I will finally return my true
08:14return.
08:15search function. So, how will my partition be? Partitions will be in the form of substrings. And I have to
08:20run a loop, which means that I will create my substrings. Every time, I have to divide my main key
08:25into two parts. One is the part in which I am going to search, and the other part in which
08:30I will recursive function, I will pass my next call as key. If I get two parts, I will return
08:36the true parts, then I will return the true parts. No, I will try to partition the next index. I
08:41will confirm my new substrings. For this, we will make code for this.
08:45What have I done in my code? I have already created a node class. I have already created a node
08:52class. I know there is a children's name area, which is 26 sizes. Because I have to store the characters
08:59here. Besides, I have a boolean variable with end of word, which is initialized with false.
09:04I have created a constructor here. I add a public constructor here. I have created a constructor in which I
09:09initialize all the children's nodes with null. I have a static node, a root, which I have initialized with an
09:14empty node. We have already seen the insert function. We have already seen how it performs. I have also seen
09:18the search operation. Now, what do I do? I will create an array of words that I will give in
09:27the beginning.
09:28First of all, I am going to create a loop with all these strings. For int i equal 0, i
09:36less than my array dot length i++. I call every time my insert function, which I am going to pass
09:42the array of i. So, every string will insert into my try. Now, the next step is for me to
09:49define my key. I am going to take a string, which I will write, i like.
09:55So, for this key, I have to do partitions and check after doing all my partitions, all my substrings forms
10:04exist in my area. So, let's create a function of public static boolean type which I will call word break.
10:14Because I am breaking my word every time, there will be a single parameter which will be my string key.
10:21The first work that I have to do is that I will start a loop for int i equals once
10:26i less than equal to my string or my key dot length i++. Now I have i is equal to
10:38one and loop key to length. This is because my substring
10:42function, I will pass my first index, beginning index, and then my last index comes. The last index is not
10:51inclusive, this is an exclusive index which means my substring form. This is not the last index character. Therefore, if
10:59I initialize from 0 and put the last index 0, my first string comes from 0,
11:06We keep the last index i. So, the value of i here is 0. So, what will happen in that
11:10case? In that case, there will be an invalid condition. Because with 0, there will not exist until minus 1.
11:16So, to avoid the error, I have started with i here. And the value of 1 here will be 1.
11:22So, in the first string, there will be 1 character in single length. Then, the next sub-string will be
11:272 characters. So, the first sub-string will be key.substring from 0
11:36to i. And the second substring will be key.substring from i.
11:42When we have our ending index in the substring function, we have to pass our last index in our string.
11:48So it is defined by default in Java, so I don't need to pass it again. I simply can write
11:53I. So what will I do as a beginning index? This is my first part and this is my second
11:59part. What did I discuss in the code? I have to call search for the first part and for the
12:04second part, recursively, I am going to call word break.
12:09This will return me true in search, meaning I have released a first part of my string and it has
12:15already been true, that this part exists. So for me it will be the easiest to check the rest of
12:20the keys that exist in there. I can break them properly in my string terms. So first of all, if
12:28condition, I will call my search function. I passed my first part in search function and
12:35I will put the and condition here and for second part, I will call my word break function. Recursive function
12:42called. First part also gave true and word break also gave true. In that case, I have to return true.
12:51And if there is no true return anywhere, then I will return the last in this case. I will summarize
12:57this. I followed a loop with i is equal to 1 to n. Every time I have divided my string
13:01into two parts. For the first part, I will search that this exists in the try. If it exists, then
13:08my word break function will be called, so I will check for the rest of the string that exists. If
13:14both have returned true, then I will return true. And until last, if I got two strings for both,
13:21I will return to false. In the base case, if my key's length is equal to 0,
13:28So what happened there? I will return true. I mean my key is in small small small small. Now my
13:35whole string is in my first part and my second part is an empty string. In that case, I have
13:41to check for first part. So what will I do with the second part? By default, I will return true
13:45from there. So it has true returned. This is my whole word break code. Now I am going to call
13:50the main function. I will pass my word break function to which I will pass my key.
14:01I have a return and I have a return I have a true and here I only do I like
14:05Sam. In this case it was true. Why? Because I exists in the string, like exists and Sam exists in
14:12the string. But here I only do I like sung. So sung doesn't exist.
14:18Only I like, but in the case, there will never be true return, so the final answer will come, that
14:23will be false. How much time did I do this? Time complexity here is big O of N is equal.
14:30In fact, I will say this is key length L is equal. As long as my key is longer, I
14:36will be longer time. So this is a linear time complexity question.