Concatenated Words - Leetcode 472 - Python

  Рет қаралды 15,734

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 43
@aditya-lr2in
@aditya-lr2in Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
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 6 ай бұрын
@@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 Жыл бұрын
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
@CostaKazistov
@CostaKazistov Жыл бұрын
Amazing how simple this solution is.
@SafalGautam-jp7ew
@SafalGautam-jp7ew Жыл бұрын
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
@geyushen4369
@geyushen4369 Жыл бұрын
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
@anu1097
@anu1097 Жыл бұрын
Time limit exceeds for 1 case. Why not use trie ?
@abrahamm1145
@abrahamm1145 Жыл бұрын
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?
@Cccccccccc7850
@Cccccccccc7850 Жыл бұрын
u shattered my confidence at the beginning lol
@satyajeetdas6577
@satyajeetdas6577 Жыл бұрын
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
@satisfyingwalks4010
@satisfyingwalks4010 Жыл бұрын
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
@MP-ny3ep
@MP-ny3ep Жыл бұрын
Thanks a ton ! Phenomenal explanation as always.
@iamnoob7593
@iamnoob7593 3 ай бұрын
Thanks neetcode
@krateskim4169
@krateskim4169 Жыл бұрын
Awesome explanation
@Kilvny
@Kilvny Жыл бұрын
Thank you so much very informative!!!
@mohithadiyal6083
@mohithadiyal6083 Жыл бұрын
Amazing explanation 👍
@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 5 ай бұрын
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 5 ай бұрын
@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 5 ай бұрын
@@AMX0013 Good luck on the next one! How many days after did they provide feedback? I am still waiting for a response.
@AMX0013
@AMX0013 5 ай бұрын
@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
@jessanraj9086
@jessanraj9086 Жыл бұрын
Wow that's an amazing explanation
@NeetCodeIO
@NeetCodeIO Жыл бұрын
Glad it was helpful!
@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
@Tom910ru
@Tom910ru Жыл бұрын
I think prefix tree can be good for this task
@tanishgotti3659
@tanishgotti3659 2 ай бұрын
Bro you didn't explain the TC rigorously.
@user-le6ts6ci7h
@user-le6ts6ci7h Жыл бұрын
I don't think the time complexity is right
@DevvOscar
@DevvOscar Жыл бұрын
What do you use to draw?
@NeetCodeIO
@NeetCodeIO Жыл бұрын
Paint3d, but I'm thinking about switching to excalidraw
@NoName-lq7xk
@NoName-lq7xk Жыл бұрын
best 🔥🔥
@aadil4236
@aadil4236 10 ай бұрын
Actually, without caching it won't pass. I tried it with js and python3.
@rashzh5502
@rashzh5502 Жыл бұрын
Nice!
@lakshmanvengadesan9096
@lakshmanvengadesan9096 Жыл бұрын
*If you are preparing for coding interviews* yes that's true, but most of the big tech companies have freezed their hirings
@CostaKazistov
@CostaKazistov Жыл бұрын
A leetcode a day keeps unemployment away
@jasonahn8658
@jasonahn8658 Жыл бұрын
@@CostaKazistov THIS
@HabibLawal
@HabibLawal Жыл бұрын
This no longer works unfortunately
@mahmudaliza4079
@mahmudaliza4079 4 ай бұрын
@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 ""
Leetcode 149 - Maximum Points on a Line - Python
8:14
NeetCodeIO
Рет қаралды 21 М.
ТЫ В ДЕТСТВЕ КОГДА ВЫПАЛ ЗУБ😂#shorts
00:59
BATEK_OFFICIAL
Рет қаралды 4,5 МЛН
ТВОИ РОДИТЕЛИ И ЧЕЛОВЕК ПАУК 😂#shorts
00:59
BATEK_OFFICIAL
Рет қаралды 6 МЛН
Молодой боец приземлил легенду!
01:02
МИНУС БАЛЛ
Рет қаралды 1,9 МЛН
Why no RONALDO?! 🤔⚽️
00:28
Celine Dept
Рет қаралды 51 МЛН
Top 7 Algorithms for Coding Interviews Explained SIMPLY
21:22
Codebagel
Рет қаралды 442 М.
Integer to English Words - Leetcode 273 - Python
20:59
NeetCodeIO
Рет қаралды 15 М.
Substring with Concatenation of All Words | Leetcode #30
13:41
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 147 М.
Concatenated Words | Simple Story | META | Leetcode 472
23:17
codestorywithMIK
Рет қаралды 7 М.
Maximum Matrix Sum - Leetcode 1975 - Python
16:07
NeetCodeIO
Рет қаралды 403
Minimum Cost to Cut a Stick - Leetcode 1547 - Python
12:15
NeetCodeIO
Рет қаралды 14 М.
Learn Python OOP in under 20 Minutes
18:32
Indently
Рет қаралды 107 М.
Mastering Dynamic Programming - How to solve any interview problem (Part 1)
19:41
ТЫ В ДЕТСТВЕ КОГДА ВЫПАЛ ЗУБ😂#shorts
00:59
BATEK_OFFICIAL
Рет қаралды 4,5 МЛН