Concatenated Words - Leetcode 472 - Python

Поділитися
Вставка
  • Опубліковано 15 гру 2024

КОМЕНТАРІ • 43

  • @aditya-lr2in
    @aditya-lr2in Рік тому +1

    I opened your yt video, saw first 5 seconds and went to code, came back after 12 mins solving it, and skipped to end to see ur code, and lmao its almost the same, even with the naming like wordset and me commenting the size of cache (30 * N * 30), thanks for the teachings !

  • @phoenix_1_3
    @phoenix_1_3 Рік тому +1

    Watched your explanation part and I went to code. First I got wrong op since I was not using a set, then after using a set, the solution got accepted. Man, you made the problem so simple for even noobs like me. You are really a legend ❤‍🔥

  • @EscapeThirty
    @EscapeThirty Рік тому +4

    APPROACH 1 IS INCORRECT
    AS A WORD CAN BE PRESENT MUTIPLE TIMES AND 2^N POSSIBILTIES CONSIDER ONLY ONE OCCURENCE OF IT THAT TOO IN ORDERED WAY

    • @NeetCodeIO
      @NeetCodeIO  Рік тому

      I think there are no duplicates in the input if that's what you meant. But yes, some of the concatenations would be individual words and we would definitely have to filter them.

    • @rowanus116
      @rowanus116 7 місяців тому

      @@NeetCodeIO I've had a hard time filtering those individual words who is not a concatenation, How could we filter those words? Thanks

  • @Avighna
    @Avighna Рік тому +4

    They recently made this harder. Immediately thought of the brute force, but it's now n

    • @PedanticAnswerSeeker
      @PedanticAnswerSeeker 6 місяців тому

      class Solution:
      def findAllConcatenatedWordsInADict(self, words: List[str]) -> List[str]:
      def is_concatenated(word, word_set, memo):
      if word in memo:
      return memo[word]
      n = len(word)
      for i in range(1, n):
      prefix = word[:i]
      suffix = word[i:]
      if prefix in word_set and (suffix in word_set or is_concatenated(suffix, word_set, memo)):
      memo[word] = True
      return True
      memo[word] = False
      return False
      word_set = set(words)
      concatenated_words = []
      memo = {}
      for word in words:
      if is_concatenated(word, word_set, memo):
      concatenated_words.append(word)
      word_set.add(word)
      return concatenated_words
      this code worked for me, so i don't think they made it different

  • @geyushen4369
    @geyushen4369 Рік тому +2

    Hard is definitely a misleading label for this question. Thought my half-brute-forcy solution was a TLE for sure, but it worked and almost the same as the official solution lol

  • @CostaKazistov
    @CostaKazistov Рік тому

    Amazing how simple this solution is.

  • @Cccccccccc7850
    @Cccccccccc7850 Рік тому

    u shattered my confidence at the beginning lol

  • @satyajeetdas6577
    @satyajeetdas6577 Рік тому +1

    We can also use Trie Data Structure for this solution which is almost very similar
    # CODE-1
    #we can optimise by not creating trie for all and create if check func returns False.
    class Trie:
    def __init__(self):
    self.list=[None]*26
    self.flag=False
    def containsKey(self,chr):
    if self.list[ord(chr)-ord('a')]!=None:
    return True
    return False
    def addkey(self,chr,node):
    self.list[ord(chr)-ord('a')]=node
    def getkey(self,chr):
    return self.list[ord(chr)-ord('a')]
    def setflag(self):
    self.flag=True
    def getflag(self):
    return self.flag
    class Solution:
    def findAllConcatenatedWordsInADict(self, words: List[str]) -> List[str]:
    self.node=Trie()

    for word in words:

    node=self.node
    for w in word:
    if not node.containsKey(w):
    node.addkey(w,Trie())
    node=node.getkey(w)
    node.setflag()
    def check(ind,node,word,count):
    curr=node
    for i in range(ind,len(word)):
    if not curr.containsKey(word[i]):
    return False
    curr=curr.getkey(word[i])
    if curr.getflag() and check(i+1,node,word,count+1):
    return True
    if count>=1 and curr.getflag():
    return True
    else:
    return False
    res=[]
    for word in words:
    if check(0,self.node,word,0):
    res.append(word)
    return res

  • @SafalGautam-jp7ew
    @SafalGautam-jp7ew Рік тому +1

    first approach of brute force might be incorrect because we do not know that whether or not the string combinations that we will make are single or concatenated.
    for example -> input: a, ab, cd
    combinations would be [aabcd, aab, acd , a, abcd, ab, cd]
    here we have a as well how can you say that whether "a" here is concatenated or not.
    so, brute forcing finding every possible solutions wouldn't make any sense

  • @anu1097
    @anu1097 Рік тому +2

    Time limit exceeds for 1 case. Why not use trie ?

  • @abrahamm1145
    @abrahamm1145 Рік тому +2

    Nice video! Im having trouble visualizing why the time complexity would be N *L^3 instead of N * L^4 when memoizing though. Does anyone have an example with a word/words that could showcase that?

  • @MP-ny3ep
    @MP-ny3ep Рік тому +3

    Thanks a ton ! Phenomenal explanation as always.

  • @satisfyingwalks4010
    @satisfyingwalks4010 Рік тому +1

    Hi, could you please make a solution video to LC 801 - "Minimum swaps to make sequences increasing" , I can't find a satisfactory video solution to that on yt

  • @iamnoob7593
    @iamnoob7593 4 місяці тому

    Thanks neetcode

  • @DipakKawale
    @DipakKawale Рік тому

    class Solution {
    Set set;
    public List findAllConcatenatedWordsInADict(String[] words) {
    this.set =new HashSet(Arrays.asList(words));
    List list = new ArrayList();
    for(String word:words){
    if(dfs(word)){
    list.add(word);
    }
    }
    return list;
    }
    public boolean dfs(String word){
    for(int i=1;i

  • @AMX0013
    @AMX0013 6 місяців тому

    Got a variation of this for Amazon phone screen. Yes I had to analyse the TC and SC. And It was modified and had to return the the compound word, and the list of combination of concatenations that would yield it. I came close, but didn't make it

    • @Gabagool22
      @Gabagool22 6 місяців тому

      i just had the same question in an Amazon phone screen, did you pass? Kind of a pretty difficult one in my opinion.

    • @AMX0013
      @AMX0013 6 місяців тому +1

      @Gabagool22 i didnt pull through. I couldnt get started but after i got to the prefix and suffix idea i think i took it along smoothly tho

    • @Gabagool22
      @Gabagool22 6 місяців тому

      @@AMX0013 Good luck on the next one! How many days after did they provide feedback? I am still waiting for a response.

    • @AMX0013
      @AMX0013 6 місяців тому

      @Gabagool22 i got the rejection the next day itself. My friend mentioned it takes
      1day for OA results
      3days for phone screen results
      5 days for loop results

  • @krateskim4169
    @krateskim4169 Рік тому

    Awesome explanation

  • @mohithadiyal6083
    @mohithadiyal6083 Рік тому

    Amazing explanation 👍

  • @Kilvny
    @Kilvny Рік тому

    Thank you so much very informative!!!

  • @jessanraj9086
    @jessanraj9086 Рік тому

    Wow that's an amazing explanation

  • @DevvOscar
    @DevvOscar Рік тому

    What do you use to draw?

    • @NeetCodeIO
      @NeetCodeIO  Рік тому

      Paint3d, but I'm thinking about switching to excalidraw

  • @Tom910ru
    @Tom910ru Рік тому

    I think prefix tree can be good for this task

  • @tanishgotti3659
    @tanishgotti3659 3 місяці тому +1

    Bro you didn't explain the TC rigorously.

  • @user-le6ts6ci7h
    @user-le6ts6ci7h Рік тому +1

    I don't think the time complexity is right

  • @NoName-lq7xk
    @NoName-lq7xk Рік тому

    best 🔥🔥

  • @rashzh5502
    @rashzh5502 Рік тому

    Nice!

  • @aadil4236
    @aadil4236 10 місяців тому

    Actually, without caching it won't pass. I tried it with js and python3.

  • @lakshmanvengadesan9096
    @lakshmanvengadesan9096 Рік тому

    *If you are preparing for coding interviews* yes that's true, but most of the big tech companies have freezed their hirings

  • @mahmudaliza4079
    @mahmudaliza4079 5 місяців тому

    @NeetCodeIO I think the brute force solution does not work. Iiuc, below is an example case with words has [cat, cats, dog, dogcat]. Can't upload screenshot. Please copy paste to some ide for better visual. The leaf has dogcat but it does not prove dogcat is a concatation of two other words dog and cat. Because cat came before dog, and we are only appending the next word, there is no combination that has dog first, then cat. And we are not considering adding same word twice i.e. catcat. Unless I am missing something. But based on video explanation, this approach does not look correct. Please update the video or comment with your feedback. Thank you.
    ""
    / \
    cat: cat ""
    / \ / \
    cats: catcats cat cats ""
    / \ / \ / \ / \
    dog: catcatsdog catcats catdog cat catsdog cats dog ""
    / \ / \ / \ / \ / \ / \ / \ /\
    dogcat: catcatsdogdogcat catcatsdog catcatsdogcat catcats catdogdogcat catdog catdogcat cat catsdogdogcat catsdog catsdogcat cats dogdogcat dog dogcat ""

  • @HabibLawal
    @HabibLawal Рік тому

    This no longer works unfortunately