Word Break Problem | Dynamic Programming | GeeksforGeeks

แชร์
ฝัง
  • เผยแพร่เมื่อ 12 ม.ค. 2025

ความคิดเห็น • 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

  • @naveenkumars3310
    @naveenkumars3310 4 ปีที่แล้ว +13

    Very clear Explanation. Thank you so much :)

  • @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 😋

  • @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 😅

  • @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.

  • @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.

  • @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.

  • @shanmukhaditya1099
    @shanmukhaditya1099 4 ปีที่แล้ว

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

  • @fahrizalsentosa1574
    @fahrizalsentosa1574 11 หลายเดือนก่อน

    Best explanation for DP about wordbreak. thank you very much

    • @tommyls4357
      @tommyls4357 10 หลายเดือนก่อน

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

    • @fahrizalsentosa1574
      @fahrizalsentosa1574 10 หลายเดือนก่อน

      @@tommyls4357should be O(n^2) bro

  • @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!

  • @rikampalkar
    @rikampalkar 2 ปีที่แล้ว

    Hats off! Very well explained, concise and dense.

  • @Integralsouls
    @Integralsouls 3 ปีที่แล้ว

    best explanation on the entire internet

  • @adityaagarwal7291
    @adityaagarwal7291 3 ปีที่แล้ว

    hand down the best explanation

  • @darkhorse621
    @darkhorse621 5 ปีที่แล้ว +1

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

  • @AbhishekKumar-yv6ih
    @AbhishekKumar-yv6ih 4 ปีที่แล้ว +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.

  • @ivailotenevv
    @ivailotenevv 4 ปีที่แล้ว +5

    Awesome , keep uploading please !!

  • @annafalvello8138
    @annafalvello8138 2 ปีที่แล้ว

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

  • @kirtimanmishra8097
    @kirtimanmishra8097 4 ปีที่แล้ว +7

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

    • @vijendrakumarsharma5250
      @vijendrakumarsharma5250 4 ปีที่แล้ว

      depends on language, for java it will work but for c++ it will be len-i

  • @AbhishekSingh-fc6tb
    @AbhishekSingh-fc6tb 4 ปีที่แล้ว +1

    Thankyou Nideesh, that was really helpful 😊

  • @jardondiego
    @jardondiego 4 ปีที่แล้ว

    Very intuitive, clear explanation, keep it up!

  • @claramartinezrubio7768
    @claramartinezrubio7768 4 ปีที่แล้ว

    Super well explained! Thank you

  • @arpitsatnalika
    @arpitsatnalika 4 ปีที่แล้ว

    Thank you so much for detailed explanation

  • @sudhakarthota880
    @sudhakarthota880 4 ปีที่แล้ว

    Nicely explained, very clear. Thanks.

  • @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.

  • @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.

  • @yosihashamen1
    @yosihashamen1 4 ปีที่แล้ว

    High-quality explanation

  • @antaratewary9645
    @antaratewary9645 3 ปีที่แล้ว

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

  • @nutan91
    @nutan91 4 ปีที่แล้ว +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.

  • @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))

  • @rahulkushwahafarzirahu
    @rahulkushwahafarzirahu 4 ปีที่แล้ว

    Great explanation, thank you so much :)

  • @goldent4655
    @goldent4655 3 ปีที่แล้ว +1

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

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

  • @raghavsurya4755
    @raghavsurya4755 4 ปีที่แล้ว

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

  • @crashcartjoe6783
    @crashcartjoe6783 ปีที่แล้ว

    Nice 👍 explanation

  • @arpitagupta5834
    @arpitagupta5834 3 ปีที่แล้ว

    nicely explained!

  • @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?

  • @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 4 ปีที่แล้ว

    Just amazing, thanks dude

  • @Aditigoyal1997
    @Aditigoyal1997 4 ปีที่แล้ว

    very nicely explained

  • @pradeepmondal4943
    @pradeepmondal4943 4 ปีที่แล้ว

    Ossm bro.keep it up..

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

  • @lpdc9767
    @lpdc9767 5 ปีที่แล้ว

    Superb, thanks!

  • @yasvidobariya1247
    @yasvidobariya1247 4 ปีที่แล้ว

    Thanks Brillian Man!

  • @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))?

  • @mushahidkhan7472
    @mushahidkhan7472 5 ปีที่แล้ว +1

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

  • @udaychopra5721
    @udaychopra5721 4 ปีที่แล้ว +3

    Doesn't anyone have audio lag for the video?

  • @chenyangwang7232
    @chenyangwang7232 4 ปีที่แล้ว

    AWESOME!!

  • @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.

  • @amansarma417
    @amansarma417 4 ปีที่แล้ว +2

    Dammm great one

  • @rohit-ld6fc
    @rohit-ld6fc 5 ปีที่แล้ว +1

    Awesome!!!

  • @umabharati9911
    @umabharati9911 4 ปีที่แล้ว +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)

  • @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 !

  • @ankurgupta4696
    @ankurgupta4696 4 ปีที่แล้ว

    why starting from index 1??

  • @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.

  • @suawasthi
    @suawasthi 5 ปีที่แล้ว

    Awesome

  • @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 ปีที่แล้ว

    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

  • @christiantith2843
    @christiantith2843 5 ปีที่แล้ว +1

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

  • @kajalpareek8291
    @kajalpareek8291 4 ปีที่แล้ว

    volume is very slow

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

  • @raheshm3856
    @raheshm3856 5 ปีที่แล้ว +4

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

  • @yobro7051
    @yobro7051 4 ปีที่แล้ว

    Very low audio

  • @lukecage8506
    @lukecage8506 5 ปีที่แล้ว +1

    Voice not audible at even full volume on earphones.

    • @rahulchaubey5227
      @rahulchaubey5227 4 ปีที่แล้ว

      Han mu me gutkha daba k bol ra aisa lag rha

  • @ashutoshdixit2806
    @ashutoshdixit2806 4 ปีที่แล้ว

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

  • @abuzaid4342
    @abuzaid4342 3 ปีที่แล้ว +1

    Is he using fake accent ?

  • @kainarmasujima9262
    @kainarmasujima9262 5 ปีที่แล้ว

    should've done coding on machine

    • @udaychopra5721
      @udaychopra5721 4 ปีที่แล้ว

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

  • @vijendrakumarsharma5250
    @vijendrakumarsharma5250 5 ปีที่แล้ว

    clapss..!!

  • @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.