Word Break Problem | Dynamic Programming | GeeksforGeeks

Поделиться
HTML-код
  • Опубликовано: 8 фев 2025
  • Find Complete Code at GeeksforGeeks Article: www.geeksforge...
    This video is contributed by Nideesh Terapalli.
    Please Like, Comment and Share the Video among your friends.
    Install our Android App:
    play.google.co...
    If you wish, translate into local language and help us reach millions of other geeks:
    www.youtube.com...
    Follow us on Facebook:
    / gfgvideos
    And Twitter:
    / gfgvideos
    Also, Subscribe if you haven't already! :)

Комментарии • 100

  • @aakupsp
    @aakupsp 5 лет назад +22

    Best explanation. It's so clear. I have watched other videos where they start with a dp matrix but don't explain the intuition behind it. This was an awesome explanation and I am never going to forget it.

    • @alisaquibraza6870
      @alisaquibraza6870 2 года назад +1

      can you link the video where they make the matrix first

  • @hsome5764
    @hsome5764 4 года назад +1

    Look no further folks. At the time of this comment this is by far the best video to watch to learn how to solve this problem because it doesn't immediately jump to an optimized solution without explaining how to get there.

  • @大盗江南
    @大盗江南 5 лет назад +41

    thank you so much... this is the video i am searching for... the best video for this question. hope u could do more videos thank you!
    very clear

  • @shalsteven
    @shalsteven Год назад

    THIS IS THE BEST EXPLANATION VIDEO! HE CAN EXPLAIN IT FROM THE INTUITION UNTIL THE CODE 🔥🔥 other peole only explain it by jumping to code directly 😅

  • @naveenkumars3310
    @naveenkumars3310 4 года назад +13

    Very clear Explanation. Thank you so much :)

  • @nickharalampopoulos
    @nickharalampopoulos 4 года назад +1

    You are very good Nideesh! You don't have to be stressed! Best tutorial with the theory properly explained. Thank you.

  • @inspired_enough_to_grow_up
    @inspired_enough_to_grow_up 5 лет назад +4

    Best video Sir... Crystal clear concept,Had been made much confusing by other tutorials.

  • @adityaagarwal7291
    @adityaagarwal7291 3 года назад

    hand down the best explanation

  • @fahrizalsentosa1574
    @fahrizalsentosa1574 Год назад

    Best explanation for DP about wordbreak. thank you very much

    • @tommyls4357
      @tommyls4357 11 месяцев назад

      What's the time complexity of the final solution, do you know?

    • @fahrizalsentosa1574
      @fahrizalsentosa1574 11 месяцев назад

      @@tommyls4357should be O(n^2) bro

  • @rikampalkar
    @rikampalkar 2 года назад

    Hats off! Very well explained, concise and dense.

  • @iamsks7
    @iamsks7 4 года назад +8

    At 16:47 instead of s.substring(i,len) i think it should b s.substring(i , len-i) if u r going for c/c++. actually it depends on how u justify using the second parameter . if u mean the index value then its ok .. but in c/c++ it means the length u want after the index i .

    • @basharattamboli3979
      @basharattamboli3979 4 года назад

      You sir are awesome... Been struggling on this last two hours. Thanks for existing 😋

  • @johnTRAN93
    @johnTRAN93 4 года назад +4

    10:30 You dont really need a map. A set would do. You can assume it always returns false If It finds a sub string you already did.

  • @christopherbrooke5980
    @christopherbrooke5980 4 года назад +1

    FABULOUS! Any video can't replace the explanation and conceptual simplicity this video has. TO_THE_POINT CONCEPTUAL VERY_CLEAR. Best video on this question I came across! Really loved it!

  • @shanmukhaditya1099
    @shanmukhaditya1099 4 года назад

    This is the best explanation I saw for word break problem. Cheers man

  • @annafalvello8138
    @annafalvello8138 2 года назад

    Thanks for this! Great explanation, really helped me understand the DP solution.

  • @darkhorse621
    @darkhorse621 5 лет назад +1

    good explanation and one of the very few resource available for word break problem.
    Highly appreciate the help !!! Cheers !

  • @arpitsatnalika
    @arpitsatnalika 4 года назад

    Thank you so much for detailed explanation

  • @arpitagupta5834
    @arpitagupta5834 3 года назад

    nicely explained!

  • @claramartinezrubio7768
    @claramartinezrubio7768 4 года назад

    Super well explained! Thank you

  • @ivailotenevv
    @ivailotenevv 4 года назад +5

    Awesome , keep uploading please !!

  • @joydeeprony89
    @joydeeprony89 4 года назад

    static bool DPWordBreak(string s, IList wordDict)
    {
    int length = s.Length;
    bool[] dp = new bool[length + 1];
    dp[0] = true;
    for (int len = 1; len

  • @Integralsouls
    @Integralsouls 3 года назад

    best explanation on the entire internet

  • @AbhishekKumar-yv6ih
    @AbhishekKumar-yv6ih 5 лет назад +1

    Brute force approach time: O(2^n) because there are n+1 ways to partition a string of length n and each of the partition can be chosen in 2 ways.

  • @sudhakarthota880
    @sudhakarthota880 4 года назад

    Nicely explained, very clear. Thanks.

  • @crashcartjoe6783
    @crashcartjoe6783 Год назад

    Nice 👍 explanation

  • @goldent4655
    @goldent4655 4 года назад +1

    Wha's the time and space complexity of the first approach with memoization?

  • @kirtimanmishra8097
    @kirtimanmishra8097 4 года назад +7

    In the DP approach, it will be list.contains(s.substring(i,len-i))

  • @rahulagrawal3611
    @rahulagrawal3611 5 лет назад +1

    s.substr(a,b), gives a sub string starting from index a and has length b. Note b is not the end index. s.substr(i,length) should be corrected to s.substr(i,len-i). Also why are we considering dp[0] as true? we have available "c" "od", "e", "x", any string of length zero isn't present in the given dictionary.
    I think we should run a separate loop for dp[1] instead of dp[0] and then we can start generic loop.

    • @avishekdutta2951
      @avishekdutta2951 4 года назад +1

      The main intuition is - the word can be broken into two halves [prefix+suffix]. Now we check if prefix exists in the given set and can we break the suffix. Lets say input word is "leet" and the wordset contains ["leet"]. In this case, the word leet can be broken into 2 halves with prefix = leet and suffix = "". now in the next level of computation if we donot return true i.e. "" is also present in the given set then the code will fail. Hence dp[0] is true.

  • @knpatel86
    @knpatel86 4 года назад +4

    Thanks for the explanation. One thing I am trying to get my head around is why an empty string is considered found in the word dictionary? This pattern is seen in many dp problems and I am really confused about it.

    • @jackie8658
      @jackie8658 4 года назад

      I was confused about it as well. After some thought, I've concluded that the empty string being considered found is misleading. dp[0] = true is used to simplify how the solution looks. I was able to solve this problem with a dp array of s.length(), but the solution GeeksForGeeks presents is a little cleaner than mine, since you don't have to subtract the index by 1 or check if the index is 0.

  • @jardondiego
    @jardondiego 4 года назад

    Very intuitive, clear explanation, keep it up!

  • @nutan91
    @nutan91 5 лет назад +3

    There is some issue with the memoization code, it takes more time than the normal recursive approach.
    You are putting in the map after the recursion call, so when recursion happens, map is still empty.
    if (set.contains(s.substring(0, i)) && wbMemo(s.substring(i, s.length()), set, map, count)) {
    map.put(s.substring(i, s.length()), true);
    Let me know your thoughts.

  • @AbhishekSingh-fc6tb
    @AbhishekSingh-fc6tb 5 лет назад +1

    Thankyou Nideesh, that was really helpful 😊

  • @yosihashamen1
    @yosihashamen1 4 года назад

    High-quality explanation

  • @kartikmahajan4405
    @kartikmahajan4405 4 года назад +1

    Sir, thanks for this video. I think that list.contains(s.substring(i, len)) should be list.contains(s.substring(i, len-i))

  • @antaratewary9645
    @antaratewary9645 3 года назад

    Please make this guy do all of your videos...please.

  • @vaib5917
    @vaib5917 4 года назад

    Simply Amazing, the best explanation came across for this question. Thanks, Man!

  • @노랑-r1d
    @노랑-r1d 4 года назад +2

    I cant hear well even at max volume. But it's good to understand. thanks.

  • @Aditigoyal1997
    @Aditigoyal1997 4 года назад

    very nicely explained

  • @rahulkushwahafarzirahu
    @rahulkushwahafarzirahu 4 года назад

    Great explanation, thank you so much :)

  • @jayshree7574
    @jayshree7574 4 года назад

    any other videos by this person? he's brilliant + the accent too

    • @siddhantsingh3392
      @siddhantsingh3392 4 года назад

      check his youtube channel. he is brilliant in addressing the intuition!

  • @varunrao2135
    @varunrao2135 5 лет назад

    Just amazing, thanks dude

  • @pradeepmondal4943
    @pradeepmondal4943 4 года назад

    Ossm bro.keep it up..

  • @chenyangwang7232
    @chenyangwang7232 4 года назад

    AWESOME!!

  • @raghavsurya4755
    @raghavsurya4755 4 года назад

    Wow man. I feel like I wanna meet you and thank you personally 😆 What an explanation! Kudos

  • @yasvidobariya1247
    @yasvidobariya1247 4 года назад

    Thanks Brillian Man!

  • @meikandanathan2923
    @meikandanathan2923 4 года назад +1

    😎 mighty one 😎

  • @jaydave2500
    @jaydave2500 4 года назад +1

    In cpp there should be s.substr(i,len-i) in last method in if condition...

    • @saumya1singh
      @saumya1singh 4 года назад +1

      Yep for recursive approach

  • @JerryHou1985
    @JerryHou1985 3 года назад

    In the dp approach, why won't we do : "map.put(s, true)", instead of map.put(stringToTest.substring(i, s.length), true))?

  • @indranilthakur3605
    @indranilthakur3605 4 года назад

    Can anyone tell me which category of DP does this question comes under? Looks to be a variation of a knapsack problem? Correct me if I am wrong.

  • @lpdc9767
    @lpdc9767 5 лет назад

    Superb, thanks!

  • @rohit-ld6fc
    @rohit-ld6fc 5 лет назад +1

    Awesome!!!

  • @amansarma417
    @amansarma417 4 года назад +2

    Dammm great one

  • @sanchiagarwal2937
    @sanchiagarwal2937 5 лет назад +10

    It should be s.substr(i,len-i) in the final code

    • @rahulagrawal3611
      @rahulagrawal3611 5 лет назад +1

      Correct..good catch

    • @kashyapdesai2727
      @kashyapdesai2727 5 лет назад

      s.substr(s,len) works fine...

    • @torrtuganooh2484
      @torrtuganooh2484 5 лет назад

      @@kashyapdesai2727 I am getting java.lang.StackOverflowError Truncated error message when using memoization using HashMap. Did anyone else also got the same?

  • @mushahidkhan7472
    @mushahidkhan7472 5 лет назад +1

    How is the run time of the memoized solution O(N^2)

  • @umabharati9911
    @umabharati9911 5 лет назад +2

    Hi Nideesh, I have a basic question. Will you solve all these questions without looking into the answer every time? I face a lot of problem trying to do these questions for the first time :O

    • @biswamohandwari6460
      @biswamohandwari6460 4 года назад

      Try to practice all questions on data structures . Once you complete it repeat the cycle. On your 4th cycle you will definitely be able to solve 90% questions and further many new questions. Thank you 😊

  • @kokonnuupoopp
    @kokonnuupoopp 4 года назад

    How will you get "CO" and Recursion of (DE) ? You are doing recursion of DE only if CO is in dictionary and CO does not exist. once you find C in dictionary you dont try "CO" You try ODE.
    The recursion happens only for
    "code"
    "ode" ( since C is in dictionary)
    "e" ( since OD is in dictionary) .I am confused. The whole premise of memoization is based on repeating recursion which I dont see happening.. can someone explain.

  • @Waruto
    @Waruto 5 лет назад

    Very good video

  • @DarkKnight-eb7pj
    @DarkKnight-eb7pj 4 года назад +1

    Can someone please help me to understand space complexity in case of memorisation approach ? I think that is exponential

    • @rishanthkanakadri414
      @rishanthkanakadri414 4 года назад

      Dark Knight memoization cannot be exponential as you donot calculate the solution of an already calculated sub problem again and again. Space complexity would be o(n) and time complexity is 0(n)

  • @suawasthi
    @suawasthi 5 лет назад

    Awesome

  • @udaychopra5721
    @udaychopra5721 4 года назад +3

    Doesn't anyone have audio lag for the video?

  • @uselesvideo
    @uselesvideo 5 лет назад

    This is an O(n**2) solution right? there is a O(n*s) one where s is the max word length.
    class Solution:
    def wordBreak(self, s: str, wordDict: List[str]) -> bool:
    wordDict=set(wordDict)
    maxLen=0
    for i in wordDict:
    maxLen=max(len(i),maxLen)

    wordStart={0}

    for i in range(len(s)):
    temp={i for i in wordStart}
    for j in wordStart:
    if i-j +1 > maxLen:
    temp.remove(j)
    elif s[j:i+1] in wordDict:
    temp.add(i+1)
    wordStart=temp
    return True if len(s) in wordStart else False
    This is my code.

  • @ankurgupta4696
    @ankurgupta4696 4 года назад

    why starting from index 1??

  • @ashminpathak7436
    @ashminpathak7436 4 года назад

    Why is dp[0] = true?

    • @vastviksewani3572
      @vastviksewani3572 4 года назад

      you can always form a string of length 0 by not choosing any word from the list.

  • @newtonsarr1234
    @newtonsarr1234 5 лет назад +1

    You need an amplifier on your voice . I should design you a special one just for your voice. Otherwise, good job and thanks !

  • @newtonsarr1234
    @newtonsarr1234 5 лет назад

    Your recursion/dfs will not pass on LeetCode

    • @newtonsarr1234
      @newtonsarr1234 5 лет назад

      @@mohdzebalam8706 I am discovering it' s better to stay away from Java substring() method

    • @newtonsarr1234
      @newtonsarr1234 5 лет назад

      @Mohammed Zeb Alam Time Limit exceeded which, I think is caused by the inefficient substring() method

  • @kajalpareek8291
    @kajalpareek8291 4 года назад

    volume is very slow

  • @yobro7051
    @yobro7051 4 года назад

    Very low audio

  • @jayshree7574
    @jayshree7574 4 года назад

    working on leetcode,
    memoization,
    map dp;
    bool WordBreakmem(string s, vector& wordDict){
    if(s.size() == 0)
    return true;
    if(dp.find(s) != dp.end())
    return dp[s];
    for(int i = 1; i

  • @christiantith2843
    @christiantith2843 5 лет назад +1

    O(n^3) right? n^2 for the loops and another n for substring. Can anyone confirm?

  • @ashutoshdixit2806
    @ashutoshdixit2806 4 года назад

    wow!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1111111

  • @raheshm3856
    @raheshm3856 5 лет назад +4

    voice is not clear even after using earphones...!!! 😐😐

  • @vijendrakumarsharma5250
    @vijendrakumarsharma5250 5 лет назад

    clapss..!!

  • @abuzaid4342
    @abuzaid4342 3 года назад +1

    Is he using fake accent ?

  • @lukecage8506
    @lukecage8506 5 лет назад +1

    Voice not audible at even full volume on earphones.

  • @kainarmasujima9262
    @kainarmasujima9262 5 лет назад

    should've done coding on machine

    • @udaychopra5721
      @udaychopra5721 4 года назад

      True, but coding on whiteboard is what they ask for in interviews

  • @hhcdghjjgsdrt235
    @hhcdghjjgsdrt235 2 года назад

    confused guy

  • @rahulchaubey5227
    @rahulchaubey5227 4 года назад

    khana khaye bina vdo bna rha h kya broo.. Thoda energetic raha kro yr.

  • @rajulshakya4899
    @rajulshakya4899 4 года назад

    can you speak a bit louder and clear?

    • @mananshah3471
      @mananshah3471 4 года назад +1

      Use CC/subtitles if you can’t comprehend. He was pretty clear.