Word Break | Leet code 139 | Theory explained + Python code

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

ความคิดเห็น • 21

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

    I tried to follow Neetcode's explanation. But it seemed to me that he actually didn't fully understand it. Which is the case when you explain something but your code follows a different rationale. Thanks for sharing your understanding!

    • @RobertPodosek
      @RobertPodosek 9 หลายเดือนก่อน

      For real, idk why he always tries to start from the end of the array.

  • @j.a.s.o.n.z.h.a.n.g
    @j.a.s.o.n.z.h.a.n.g 3 ปีที่แล้ว +6

    I literally watched like four DP explanations, finally clicked at yours!!

  • @MahmudulHasan-nq4th
    @MahmudulHasan-nq4th ปีที่แล้ว

    Great explanation! Thank you.

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

    For all those getting list index out of range error, try this.
    class Solution:
    def wordBreak(self, s: str, wordDict: List[str]) -> bool:
    dp = [True] + [False] * len(s)

    for i in range(1, len(s) + 1):

    for j in range(i):
    if dp[j] and s[j:i] in wordDict:
    dp[i] = True
    break
    return dp[-1]

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

    Good explanation of the solution! Thank you

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

    what is the time complexity of endswith?

  • @RobertPodosek
    @RobertPodosek 9 หลายเดือนก่อน

    I keep getting list index out of bounds on 'dp[indx - len(word)]'

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

    you can have a break inside the if statement too

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

    Thank you very much for a detailed solution

  • @-SiddharthaChappidi
    @-SiddharthaChappidi 3 ปีที่แล้ว +2

    class Solution:
    def wordBreak(self, s: str, wordDict: List[str]) -> bool:
    matrix=["True"]+["False"]*len(s)
    index=0
    for i in range(0,len(s)):
    word=s[index+1:i]
    if word in wordDict:
    matrix[i]="True"
    index=i
    else:
    matrix[i]="False"
    return(matrix[-1])
    bro its giving correct output in programiz,but wrongoutput in leetcode for the same test case:s="catsandog"
    wordDict=["cats","dog","sand","and","cat"].can i know reason ?

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

    Superb solution.. Thanks for this video

  • @-Corvo_Attano
    @-Corvo_Attano ปีที่แล้ว

    *JAVA SOLUTION*
    class Solution {
    HashSet set = new HashSet();
    public boolean wordBreak(String s, List wordDict) {
    if(s.length() == 0) return true;
    if(set.contains(s)) return false;
    for(String str:wordDict){
    if(s.indexOf(str) == 0){
    if(wordBreak(s.substring(str.length()),wordDict)) return true;
    }
    }
    set.add(s);
    return false;
    }
    }

  • @tenzin8773
    @tenzin8773 4 ปีที่แล้ว +1

    Great video and explanation Anish! In terms of time complexity is it O(w*n) where "w" is the number of words in the wordDict and "n" is the length of the word?

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

    Thanks for this :D