Word Break Problem Dynamic Programming

Sdílet
Vložit
  • čas přidán 27. 07. 2024
  • Given a string and a dictionary, return true if string can be split into multiple words such that each word is in dictionary. If not return false
    github.com/mission-peace/inte...
    github.com/mission-peace/inte...

Komentáře • 165

  • @alishoman2826
    @alishoman2826 Před 4 lety +141

    I think dictionary is {"I" , "am " , "ace" , "a" }

  • @yushdecides
    @yushdecides Před 3 lety +87

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

    • @jacobstech1777
      @jacobstech1777 Před 3 lety +3

      hahahaha

    • @LightningXThunderVlogs
      @LightningXThunderVlogs Před 3 lety +4

      typical english dict works

    • @yushdecides
      @yushdecides Před 3 lety +2

      @@LightningXThunderVlogs
      You have to check whether words are in dictionary or not. For that you need to have a map and store those words in that map.
      You can't store all the words in the English dictionary in the map manually and that's why you need to have a dictionary given in the question which this guy hasn't given.

    • @LightningXThunderVlogs
      @LightningXThunderVlogs Před 3 lety +6

      @@yushdecides it was assumed english dict is the map in video

    • @yushdecides
      @yushdecides Před 3 lety

      @@LightningXThunderVlogs Bro but you can't use that in code.
      +, in general, when these types of questions are framed, it is assumed, a particular dictionary(given) is being followed

  • @muskanroxx22
    @muskanroxx22 Před 3 lety +14

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

  • @9351379627
    @9351379627 Před 9 lety +185

    You should have also mentioned your dictionary.

    • @maciejgirek1241
      @maciejgirek1241 Před 5 lety +13

      @A Hero Academia Then why 'm' does not belong to dictionary ?

    • @ZeAldhyr
      @ZeAldhyr Před 5 lety +2

      @@maciejgirek1241 This letter doesn't constitue a word nor an idiom, hence it doesn't belong in a dictionnary.

    • @stevenjchang
      @stevenjchang Před 5 lety

      @Chetan no, because 'mace' and 'mac' are in the English dictionary but in his example they are not in his dictionary

    • @zonito
      @zonito Před 4 lety +1

      @@stevenjchang Consider that it is custom dictionary. you can simulate with mace true. the solution will still work. I a mace - Valid!

    • @AkashSingh-ov7oe
      @AkashSingh-ov7oe Před 4 lety

      @@maciejgirek1241 because 'm' is a letter and not a word. You cannot compare 'm' with 'a' because a also acts as an article which has a meaning.

  • @sayasark
    @sayasark Před 4 lety +25

    And where is the dictionary.

  • @bhavukmathur2709
    @bhavukmathur2709 Před 8 lety +6

    I've never left your video unsatisfied. Great work, thanks :)

    • @bhavukmathur2709
      @bhavukmathur2709 Před 8 lety

      +Tushar Roy
      Just a suggestion, you can have more views if you also include the algorithm at the end of the video. I guess just a single page would do.

    • @bhavukmathur2709
      @bhavukmathur2709 Před 8 lety

      Great then :)

    • @sumitachauhan1050
      @sumitachauhan1050 Před 7 lety

      Thanks for this video lesson. Where can I find your latest video of the same word segmentation problem?

  • @amarnathprasad9986
    @amarnathprasad9986 Před 2 lety +2

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

  • @niyatitiwari9967
    @niyatitiwari9967 Před 4 lety +1

    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.

  • @prekshakoirala6331
    @prekshakoirala6331 Před 7 lety +8

    Can we also use recursion with memoization for this problem?

  • @avinashmv9686894578
    @avinashmv9686894578 Před 5 lety

    Brilliant as always. Thanks!

  • @infinitequest2540
    @infinitequest2540 Před 9 lety +4

    Hi Tushar,
    Nice vid! But it would be more helpful to understand if you could walk through the code posted on your github account. Thanks

  • @starc701
    @starc701 Před 4 lety +13

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

  • @ShekharPrasadRajak
    @ShekharPrasadRajak Před 9 lety +1

    New era of interview Programmming channel in youtube :D

  • @iamdipankarj
    @iamdipankarj Před 9 lety +1

    What to do If I want to list all combinations?
    Like, the input string is = "Thisishugebreakthrough". The dictionary is ["This", "is", "huge", "break", "through", "breakthrough"]. Here 2 combinations are possible.

  • @jaatharsh
    @jaatharsh Před 3 lety +5

    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 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

  • @bhavukmathur2709
    @bhavukmathur2709 Před 8 lety +2

    Is this solution with O(m^3) the most efficient algorithm?

  • @harshpanwar1550
    @harshpanwar1550 Před 3 lety

    Thanks a lot!!! Very well explained.

  • @shivankchaturvedi4817
    @shivankchaturvedi4817 Před 3 lety +2

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

  • @zackgow1960
    @zackgow1960 Před 7 lety +2

    Your videos are super helpful. I've been wondering though, where did you get your hoodie from?

  • @kkaabbccdd8080
    @kkaabbccdd8080 Před 6 lety +1

    I would expect you to explain why you choose DP. Second explain what does the 2-D matrix is built on and why storing Bool values in it.

  • @coldstone87
    @coldstone87 Před 2 lety

    I just love you man. Brilliant

  • @codedown7145
    @codedown7145 Před 8 lety

    Thanks Tushar. Very interesting question, nicely explained.
    An extension to the question could be: how do we process the input if it is really large and cannot be fetched at a time?
    Ex:
    "thisisareallylargestring"
    If we partition at the right place ("thisisareally" and "largestring") than it works well, but if we partition half word ("thisisareall" and "ylargestring") than its a problem.
    ...any thoughts?

  • @rituagrawal2218
    @rituagrawal2218 Před 7 lety

    Very nice explanation . Thanks Tushar

  • @kaustubh221085
    @kaustubh221085 Před 2 lety +1

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

  • @eaterofzawarudo6764
    @eaterofzawarudo6764 Před 5 lety

    how would you find the number of ways to express a word? My strategy was:
    dp[i][j]=isAWord?1:0;
    for(int k=i; k

  • @ujjvalpatel5353
    @ujjvalpatel5353 Před 7 lety +5

    just like Matrix chain multiplication .

  • @mihirkumarsrivastava1895

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

  • @jiiiina5244
    @jiiiina5244 Před 5 lety

    thanks as always!!

  • @siddharthsethia5986
    @siddharthsethia5986 Před 6 lety

    You rock man!

  • @alanliang9538
    @alanliang9538 Před 4 lety

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

    • @alanliang9538
      @alanliang9538 Před 4 lety

      litterally can solve the halting problem with his table

    • @amanrubey
      @amanrubey Před 2 lety

      Exactly! He's superb

  • @princeakhil208
    @princeakhil208 Před 3 lety +3

    Yes we use dynamic programming to solve this !

  • @jhc25yt
    @jhc25yt Před 9 lety

    Very clear explanation, thank you!

  • @pittala1
    @pittala1 Před 7 lety

    The intuition for splitting a[i][j] at k, can be better explained like this:
    In a[i][j], start looking from indexes m,n,... for words of different lengths: a[i][i], a[i+1][j], then a[i][i+1], a[i+2][j] ... until a[i+j-1][aj][j], like how we would search in a given long word. Lets say there are two splits with valid first word. Lets call them splits at m & n. We would then check whether a[m+1][j] or a[n+1][j] is valid by checking into the previously computed matrix (bottom up order)

  • @ZaverRabadiya14
    @ZaverRabadiya14 Před 8 lety +2

    I don't understand what is repeating problem here? It would make sense if you would draw a recursion tree and spot out the repeating subproblems

    • @shubhamrajput2667
      @shubhamrajput2667 Před 7 lety

      Actually that is , you always not need to compute all the string if u have already computed it's subproblem and stored it.(this is funda for DP)

  • @maxlin1857
    @maxlin1857 Před 8 lety

    Is it able to solve this question using 1 dimension array instead of 2 ?

  • @shimanshu
    @shimanshu Před 9 lety +5

    3:25 , a is 3-3 but 0-0

  • @jaskaransethi2000
    @jaskaransethi2000 Před 4 lety

    This is a very good explanation. Thank you for your valuable time.

  • @effy1219
    @effy1219 Před 7 lety

    Great work

  • @rongrongmiao4638
    @rongrongmiao4638 Před 7 lety +1

    Why is this solution O(n^3)? We're only filling half of a 2D table here, shouldn't the runtime be O(n^2)?

    • @ivandrofly
      @ivandrofly Před 3 měsíci

      See the code in the description

  • @aalsicoder2333
    @aalsicoder2333 Před 8 lety +3

    hey tushar!! how about using a trie for the same problem?? trie would result a better solution.

    • @aalsicoder2333
      @aalsicoder2333 Před 8 lety

      *****
      complexity of search in a trie would be o(26*length of pattern to be searched in dictionary
      )

    • @abhishes
      @abhishes Před 6 lety +1

      More than complexity. the solution with Trie is easier to understand. The problem with DP is that tomorrow if someone is reading the code its impossible to understand what the matrix update at every step is doing.

  • @vipulvikram6574
    @vipulvikram6574 Před 5 lety

    Nice explanation.

  • @prabhamuthu5345
    @prabhamuthu5345 Před 9 lety

    thanku so much..i have started liking dynamic prog because of u..

  • @superparkourist215
    @superparkourist215 Před 5 lety

    A mace is a weapon used in medieval combat.

  • @hamsalekhavenkatesh3440
    @hamsalekhavenkatesh3440 Před 5 lety +1

    else T[I][J] is false :) in the last line of recurrence relation....very well explained!

  • @chongsun7872
    @chongsun7872 Před 5 lety +1

    Interesting. I have been dealing with DP matrix where you need to fill up in a strictly bottom-up manner (vertically). This is the first problem I've seen that you need to fill it up diagonal by diagonally. Great explanation though!

    • @VirajMavani
      @VirajMavani Před 4 lety

      @Shashank Kumar You might want to check your math once again. This is an O(n^3) solution.

    • @coldstone87
      @coldstone87 Před 2 lety

      @@VirajMavani you got better solution?

  • @RaviKumar-vk6ib
    @RaviKumar-vk6ib Před 4 lety

    awesome brother ...

  • @efagonzaguatemala
    @efagonzaguatemala Před 7 lety +6

    What is the recurence relation, Mr. Roy?
    Thanks in advance for your answer.

  • @VadayKapeed9999
    @VadayKapeed9999 Před 3 lety

    What would be the time complexity of this solution?

  • @ravisekharchegondi6279

    Thanks Tushar for the explanation.

  • @josephluce3801
    @josephluce3801 Před 6 lety

    There is a problem. What if you get a dictionary of {'aa', 'aaaa'} and your word is aaaaaaa. (7 a's). The logic he described does not satisfy this case. He missed the logic condition when you check if the substring exists in the dictionary AND also check if the substring before it is also true before setting true.

  • @mickychan6387
    @mickychan6387 Před 2 lety +1

    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

  • @PhaniThejaSwarupVempalli
    @PhaniThejaSwarupVempalli Před 9 lety +1

    Thanks Tushar. Can you please explain the time complexity of this approach?

    • @iniyanp
      @iniyanp Před 9 lety

      ***** Can you explain how it is O(m^3)? Thanks.

    • @amitjaiswal1
      @amitjaiswal1 Před 9 lety +1

      Iniyan Paramasivam look at the equation at the end, there is clearly a loop over i, inside which there is a loop over j, and further, inside j, there is a loop over k. so, n^3.

    • @bhavukmathur2709
      @bhavukmathur2709 Před 8 lety

      +Tushar Roy
      Is this solution with O(m^3) the most efficient algorithm?

  • @rohittakalkar4431
    @rohittakalkar4431 Před 3 lety

    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

  • @rajarshibasak559
    @rajarshibasak559 Před 4 lety +1

    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.

  • @Derpocracy
    @Derpocracy Před 6 lety +22

    "does MACE belong to the dictionary? no, let's say no"

    • @Aaron-vr4yr
      @Aaron-vr4yr Před 4 lety +2

      I'm gonna pretend I didn't see that

  • @rajeshd7389
    @rajeshd7389 Před 6 lety +4

    I think You should mention your dictionary first(Like "mace" also comes in dictionary ) ... else explanation is good !!

  • @bogdyee
    @bogdyee Před 7 lety +3

    This problem is also solavable with 1 dimension array

    • @ujjvalpatel5353
      @ujjvalpatel5353 Před 7 lety +1

      How ?

    • @WowPlusWow
      @WowPlusWow Před 3 lety

      👀

    • @bogdyee
      @bogdyee Před 3 lety +1

      @@ujjvalpatel5353 a little late to the convo, but its something like this
      dp[0] = 1
      for (i = 1; i = 1; j--)
      if(is_word(j, i))
      dp[i] = dp[j - 1];
      if(dp[str_size] == 1)
      printf("The string can be splitted");
      else
      printf("The string cannot be splitted");
      Also keep in mind that in my case, the string is indexed from 1.

  • @amanpreetsinghbawa1600

    simple and awesome explanation

  • @MrGaurav1007
    @MrGaurav1007 Před 9 lety

    Can you tell me algorithm of it?

  • @pawantiwary1957
    @pawantiwary1957 Před 6 lety

    how come 'm' is not a word in dictionary but 'a' is ??? Please explain

  • @nikhil_squats
    @nikhil_squats Před 3 lety +1

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

  • @ashishkumarkunar7178
    @ashishkumarkunar7178 Před 4 lety

    Very good sir

  • @tempstudierchannel1141
    @tempstudierchannel1141 Před 8 lety +7

    mace is a word, pretty good explanation though

  • @Ap-dv6lg
    @Ap-dv6lg Před 5 lety

    very good explanation!!

  • @ratandeepsingh4850
    @ratandeepsingh4850 Před 3 lety +1

    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

  • @muskanroxx22
    @muskanroxx22 Před 3 lety

    DP will be the death of me!!

  • @zahidgul6337
    @zahidgul6337 Před 5 měsíci

    what is the string and what is the dictionary ?

  • @hobokenbloomfiled8356
    @hobokenbloomfiled8356 Před 9 lety

    What words are in the dictionary?

  • @minhnguyen-te4eb
    @minhnguyen-te4eb Před 8 lety +4

    why 'c' and 'e' not in dictionary at the beginning?

    • @ravitejasrigiriraju
      @ravitejasrigiriraju Před 8 lety +2

      because a is an article in english and you can find it in a dictionary and the same applies to I, but not for m ,c,e.

    • @BatehamRadio
      @BatehamRadio Před 6 lety +2

      In english, 'A' is an actual word. 'C' and 'E' are just letters of the alphabet.

  • @abhaypatil2000
    @abhaypatil2000 Před 4 lety

    What is the time complexity????

  • @VijaySharma-hw4kv
    @VijaySharma-hw4kv Před 6 lety

    3:22 why a is considered as [0,0] ??

  • @anuragmaurya3805
    @anuragmaurya3805 Před 6 lety +1

    you should try to print all possible word breaks not just return true and print only one result..this problem basically belongs to backtracking and recursion..though i appreciate the dp approach

  • @zealkapadiya1096
    @zealkapadiya1096 Před 3 lety

    please add recursion tree in your video too...

  • @anssha2643
    @anssha2643 Před 6 lety

    which dictionary is he referring to?

  • @SmileyMPV
    @SmileyMPV Před 7 lety +3

    But this is O(n^3) time and I came up with an algorithm that I think takes O(n^2) time.

  • @garethbale1943
    @garethbale1943 Před 4 lety

    So you are the Fire Fist Ace

  • @apoorvkrishna7328
    @apoorvkrishna7328 Před 4 lety

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

  • @abhishekhanda1622
    @abhishekhanda1622 Před 6 lety

    Not optimized DP solution ,can be even further optimized..

  • @yanivgardi9028
    @yanivgardi9028 Před 8 lety

    thanks Tushar for another great video.
    Just to emphasize - The purpose of the problem is to answer if the string can be break into words.
    (not like other DP problems where we need to find minimum or maximum cost, etc).
    However, it should be mentioned that the optional break, suggested at the end is only one option out of many possible options, that can be discovered if we keep checking for ALL possible break-points in every substring we handle

  • @suhasnayak4704
    @suhasnayak4704 Před 5 lety

    What is your dictionary ?

  • @abhishekkumargupta3043
    @abhishekkumargupta3043 Před 3 lety +1

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

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

    dp[0]=1; int end;

    for(int start=0;start

  • @amanshukla2030
    @amanshukla2030 Před 4 lety

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

    • @amanshukla2030
      @amanshukla2030 Před 4 lety

      public class Solution {
      public int wordBreak(String s, ArrayList B) {
      boolean[] t = new boolean[s.length()+1];
      t[0] = true;
      Set dict = new HashSet(B);
      for(int i=0; i s.length())
      continue;
      if(t[end]) continue;
      if(s.substring(i, end).equals(a)){
      t[end] = true;
      }
      }
      }
      return t[s.length()] ;
      }
      }

  • @avinash_1341
    @avinash_1341 Před 9 lety

    Bhai board k saamne khade hoge toh kaise dikhega? -_-

  • @pownarthithimiridayasagar2229

    Hello Sir,
    Thank you so much for the videos. Your service is definitely helping thousands of us to secure our dream job.
    I have a question about this solution. When I was looking at different approaches for this problem, I came across the below solution,
    Map memoized;
    String SegmentString(String input, Set dict) {
    if (dict.contains(input)) return input;
    if (memoized.containsKey(input) {
    return memoized.get(input);
    }
    int len = input.length();
    for (int i = 1; i < len; i++) {
    String prefix = input.substring(0, i);
    if (dict.contains(prefix)) {
    String suffix = input.substring(i, len);
    String segSuffix = SegmentString(suffix, dict);
    if (segSuffix != null) {
    memoized.put(input, prefix + " " + segSuffix);
    return prefix + " " + segSuffix;
    }
    }
    memoized.put(input, null);
    return null;
    }
    This approach seems to be simple with time complexity of O(n^2). Could you please tell us how your solution differs from this.

  • @kumarshrey209
    @kumarshrey209 Před 4 lety

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

  • @ShaliniNegi24
    @ShaliniNegi24 Před 3 lety

    This can be solved using 1-D dp.

  • @satyajeetjha1681
    @satyajeetjha1681 Před 7 lety

    why don't you first explain your dp function ?

  • @sidneyjohnson7406
    @sidneyjohnson7406 Před 6 lety

    daddy

  • @crazyboy-gw7rk
    @crazyboy-gw7rk Před 3 lety

    Bhai dictionary kaha hai tumhara ?

  • @pinigantikrishnavamsi7779

    How many of u r watching in this Lockdown

  • @ryansadeghi3824
    @ryansadeghi3824 Před 7 lety

    one's again Hamshahry let me now "Forbedden" for what?

  • @JoffreyB
    @JoffreyB Před 2 lety +1

    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.

  • @Decessus117
    @Decessus117 Před 4 lety +1

    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.

  • @sudhakartripathi3879
    @sudhakartripathi3879 Před 2 lety

    where is dictionary ???

  • @mohitsoni2919
    @mohitsoni2919 Před 5 lety +1

    Not properly explained.

  • @jayyanthmalepati7584
    @jayyanthmalepati7584 Před rokem

    where is the dictionary😒😒

  • @Aysh_Sach_16-2
    @Aysh_Sach_16-2 Před 4 lety +1

    bit confusing

  • @asthavijayvargiya4845
    @asthavijayvargiya4845 Před 3 lety

    it's 6x6 matrix not 5x5

  • @jesuyedavid9497
    @jesuyedavid9497 Před 6 lety

    Shorter python solution: gist.github.com/tabularrasa/aa5c9668afaf52c11584d175ff223f66

  • @alex_smallet
    @alex_smallet Před rokem

    I am sorry, this is the most confusing explanation. Not to mention the fact that there is a mistake at 3:25. When you are asking "Does 'a' belong to the dictionary?" and pointing to 0-0, while you should refer to the cell 3-3 After watching this multiple times I was not able to understand the idea and referred to another video, which uses one line of dp and is much easier to understand.