Word Break Problem Dynamic Programming

Word Break Problem Dynamic Programming

182,348 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@sayasark
@sayasark - 11.11.2019 21:42

And where is the dictionary.

Ответить
@rajarshibasak559
@rajarshibasak559 - 29.11.2019 07:44

String="iamace" Words=[i, am, ace]
1) take array of length of the string+1.and first cell is empty string.
2) take first character check in Words it's there or not.
If yes empty string in the cell.
If no that character plus previous cell string and keep in the cell.
3) if not empty string in cell check any word in Words is matching with it or not if match empty string in cell or keep as it is.
4) continue for rest character.
5) in the last cell if it's empty then successful else not.

Ответить
@alanliang9538
@alanliang9538 - 16.01.2020 13:12

tushar litterally can solve any problem in the world with his table

Ответить
@starc701
@starc701 - 05.02.2020 13:39

bro please read comments....................everyone is saying same thing , just put it (your holy dictionary )in the discription atleast

Ответить
@inspired_enough_to_grow_up
@inspired_enough_to_grow_up - 08.02.2020 21:02

bit confusing

Ответить
@garethbale1943
@garethbale1943 - 02.04.2020 00:47

So you are the Fire Fist Ace

Ответить
@rahulchaubey5227
@rahulchaubey5227 - 11.04.2020 15:20

Worst explaination.

Ответить
@apoorvkrishna7328
@apoorvkrishna7328 - 03.05.2020 21:47

what is the time complexity?
O(n^4) cause string matching will take O(n) and O(n^3) for filling the matrix

Ответить
@Decessus117
@Decessus117 - 20.05.2020 17:04

Yeah as people pointed out, you seem to be operating with a reduced/arbitrary dictionary. For example, I would identify {I, a, am, ma, mac, ace, mace} as the dictionary ("ma" as in mother). The real value of the word break problem, I think, is to show that you can arrive at multiple valid solutions ("I a mace"/"I am ace"). In this case it's not the job of DP to actually choose which solution is better, so by reducing your dictionary I think you missed out on that teaching opportunity. Just saying.

Ответить
@pinigantikrishnavamsi7779
@pinigantikrishnavamsi7779 - 30.05.2020 08:13

How many of u r watching in this Lockdown

Ответить
@RaviKumar-vk6ib
@RaviKumar-vk6ib - 06.06.2020 19:25

awesome brother ...

Ответить
@BRBallin1
@BRBallin1 - 07.06.2020 09:22

Not one of your best videos Tushar

Ответить
@niyatitiwari9967
@niyatitiwari9967 - 17.06.2020 08:39

Thank you so much for the lectures. I have been trying to understand DP for ages now, never found better explanations. Finally got a grasp on how to approach the problems.

Ответить
@amanshukla2030
@amanshukla2030 - 15.07.2020 21:36

This can be done in a 1D array of DP instead of 2D DP array.

Ответить
@kumarshrey209
@kumarshrey209 - 30.07.2020 13:01

I,am,a,ace are words in the dictionary

Ответить
@umadbruh4776
@umadbruh4776 - 07.08.2020 21:13

Is nobody going to talk about how he just disregarded "mac" and said it doesn't exist. Lol he works in apple too.










It's a joke ppl get yo burnt ass outta here

Ответить
@abhaypatil2000
@abhaypatil2000 - 15.08.2020 21:38

What is the time complexity????

Ответить
@mihirkumarsrivastava1895
@mihirkumarsrivastava1895 - 27.08.2020 13:17

thanks a lot sir you really helped me in this question, once again thanks

Ответить
@abhishekkumargupta3043
@abhishekkumargupta3043 - 29.08.2020 10:46

Here's my C++ O(n) space solution:
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
int n=s.size();

bool dp[n+1];
memset(dp,0,n+1);

dp[0]=1; int end;

for(int start=0;start<n;start++)
{
if(!dp[start])
continue;

for(string word:wordDict)
{
end=start+word.length();
if(s.substr(start,word.length())==word)
dp[end]=1;
}
}

return dp[n];
}
}

Ответить
@ratandeepsingh4850
@ratandeepsingh4850 - 18.09.2020 09:28

Nice. It can also be done with 1-D array. If anyone is looking for that, here it is. Took me a while to figure out.
s-input string, dict- dictionary converted to hashset.

boolean[] f = new boolean[s.length() + 1];

f[0] = true;
for(int i=1; i <= s.length(); i++){
for(int j=0; j < i; j++){
if(f[j] && dict.contains(s.substring(j, i))){
f[i] = true;
break;
}
}
}
return f[s.length()];

Ответить
@shivankchaturvedi4817
@shivankchaturvedi4817 - 24.09.2020 21:11

the best explanation except it took me time to understand what is in the dictionary

Ответить
@nikhil_squats
@nikhil_squats - 29.09.2020 09:23

Dictionary is - {I,A,am,ace}

Ответить
@princeakhil208
@princeakhil208 - 29.09.2020 19:10

Yes we use dynamic programming to solve this !

Ответить
@jaatharsh
@jaatharsh - 30.09.2020 00:43

thanks again for superb video but found a couple of issues with ur code for Tabulation method & especially Printing but thanks for explaining the solution enough so i was able to figure out the required change.
Solution in C#
// Bottom-Up Tabulation based solution // Time O(n^3) || Space O(n^2)
// Function which returns true if for given input there exists such a set where each substring is present in the dictionary & also prints selected Words
// Ex: for string "code" returns true if dictionary contains 'c','od','e' or excat word "code"
public static bool WordBreakProblemTabulation(string input, HashSet<string> dictionary)
{
var len = input.Length;
if (input == null || len < 1) return false;

int[,] tab = new int[len, len];

for (int i = 0; i < len; i++)
for (int j = 0; j < len; j++)
tab[i, j] = -1; // set default -1 to indicate word/substring of words not found in Dictionary

for (int size = 1; size <= len; size++) // size of current substring start from 1 to entire length of input
for (int i = 0; i <= len - size; i++) // start index in input from where we consider substring
{
int j = i + size - 1; // last index in input till where we consider substring
var substring = input.Substring(i, size); // extract string for ease of use

if (dictionary.Contains(substring)) // if given substring exists in dictionary mark this True and continue onto next string
{ tab[i, j] = j; continue; }

// looking for an index i.e. 'splitAT' so that both left & right substring return true
for (int splitAt = i; splitAt <= j; splitAt++)
if (tab[i, splitAt] != -1 && tab[splitAt + 1, j] != -1)
{ tab[i, j] = splitAt; break; }
}

// if subsets of words for input which exists in Dictionary found print them & return True
var found = tab[0, len - 1] != -1;
if (found) PrintWordsInWordBreakProblem(tab, input, len);
return found;
}

// Time O(n)
public static void PrintWordsInWordBreakProblem(int[,] arr, string input, int len)
{
Console.Write($"\n Words which were found in dictionary are: \t");
int i = 0, j = len - 1, k;
while(i<=j)
{
k = arr[i, j];
if (j == k) // entire word found, print word & exit
{
Console.Write($" \t{input.Substring(i, j - i + 1)}");
break;
}
else // else print left half n to continue to search for remaining words in the right half
{
var p = Math.Max(k, 1); // needed hence we are printing character on 0th index hence add 1 to length
Console.Write($" \t{input.Substring(i, p)}");
}
i = k + 1; // keeping interating on right half
}
}

Ответить
@VadayKapeed9999
@VadayKapeed9999 - 30.09.2020 13:21

What would be the time complexity of this solution?

Ответить
@ROSHANKUMAR-rl6bf
@ROSHANKUMAR-rl6bf - 12.10.2020 17:52

slower soln

Ответить
@yushdecides
@yushdecides - 12.10.2020 20:41

Lol I can't believe he explained the whole question without telling the exact dictioniory

Ответить
@ShaliniNegi24
@ShaliniNegi24 - 18.10.2020 09:08

This can be solved using 1-D dp.

Ответить
@rohittakalkar4431
@rohittakalkar4431 - 04.11.2020 16:58

perfect explaination . Dictionary is "i, am, a, ace" (mentioning this for the ones who understood the approach but did not got whats the dictionary) lol

Ответить
@DeepakKumar-yc9hr
@DeepakKumar-yc9hr - 05.11.2020 05:08

Please my friends Dislike the video

Ответить
@SimranGupta-pz7nw
@SimranGupta-pz7nw - 26.12.2020 14:25

Below code might be helpful for the viewers.

class Sol
{
public static int wordBreak(String A, ArrayList<String> B )
{
int n = A.length();
int dp[][] = new int[n][n];

HashSet<String> Dic = new HashSet<>(B);

int k=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n && k<n && j+k<n;j++)
{
String w = A.substring(j,j+k+1);
if(Dic.contains(w))
{
dp[j][j+k] = 1;
continue;
}


for(int x =j; x<j+k ; x++)
{

if( dp[j][x]==1 && dp[x+1][j+k]==1)
dp[j][j+k]=1;

}

}
k++;

}
return dp[0][n-1];
}
}

Ответить
@zealkapadiya1096
@zealkapadiya1096 - 18.01.2021 18:23

please add recursion tree in your video too...

Ответить
@asthavijayvargiya4845
@asthavijayvargiya4845 - 10.03.2021 07:06

it's 6x6 matrix not 5x5

Ответить
@crazyboy-gw7rk
@crazyboy-gw7rk - 15.04.2021 15:28

Bhai dictionary kaha hai tumhara ?

Ответить
@muskanroxx22
@muskanroxx22 - 27.05.2021 10:53

Tushar: "yes we will use dynamic programming to solve this"
me: but why?
Tushar: "keh diya na, bas, keh diya"

Ответить
@muskanroxx22
@muskanroxx22 - 27.05.2021 10:54

DP will be the death of me!!

Ответить
@harshpanwar1550
@harshpanwar1550 - 28.05.2021 13:32

Thanks a lot!!! Very well explained.

Ответить
@genki316
@genki316 - 28.07.2021 03:33

This has to be one of the worst Work Break explanations. first of all the DP matrix you are Say A is a word in the dictionary... What Dictionary? then you say C isn't?

Ответить
@sudhakartripathi3879
@sudhakartripathi3879 - 19.08.2021 22:39

where is dictionary ???

Ответить
@mickychan6387
@mickychan6387 - 30.09.2021 22:04

This is what real content on education looks like on the internet. No funky thumbnails with faang logos, nor any kind of fake publicity. Just professional teaching. Hats off Sir and Thank you

Ответить
@amarnathprasad1
@amarnathprasad1 - 27.10.2021 16:43

Wife: Honey, I've got myself into a prob...
Tushar: Yes, we will use dynamic programming to solve this problem.

Ответить
@kaustubh221085
@kaustubh221085 - 14.02.2022 19:56

What is the worst case complexity for this solution? Is it O(n^3), since there are 3 loops?

Ответить
@coldstone87
@coldstone87 - 20.06.2022 16:16

I just love you man. Brilliant

Ответить
@JoffreyB
@JoffreyB - 31.07.2022 14:18

It would've been nice if you actually wrote what IS the dictionary you're talking about. I mean explicitly specify the words in a dictionary and a dictionary itself. Otherwise you're just giving the input word and I personally have no idea with what dictionary words you're doing your matching, because I see only 1 input word.

Ответить
@jayyanthmalepati7584
@jayyanthmalepati7584 - 18.09.2022 21:17

where is the dictionary😒😒

Ответить
@ayushaggarwal7690
@ayushaggarwal7690 - 17.12.2023 08:00

Don't learn DP from this guy, you won't be able to develop the intuition.

Ответить
@zahidgul6337
@zahidgul6337 - 18.02.2024 02:20

what is the string and what is the dictionary ?

Ответить
@ivandrofly
@ivandrofly - 04.04.2024 12:42

There is a bug on solution for this video on github on method at line #60 - one line 73 you are subscring too much

Ответить
@aruneshsrivastava7154
@aruneshsrivastava7154 - 26.12.2024 10:00

where is the fucking dictionary

Ответить