Implement Trie (Prefix Tree) - Leetcode 208

Ғылым және технология

🚀 neetcode.io/ - A better way to prepare for Coding Interviews
🐦 Twitter: / neetcode1
🥷 Discord: / discord
🐮 Support the channel: / neetcode
Twitter: / neetcode1
Discord: / discord
Coding Solutions: • Coding Interview Solut...
Dynamic Programming Playlist: • House Robber - Leetco...
Tree Playlist: • Invert Binary Tree - D...
Linked List Playlist: • Reverse Linked List - ...
Problem Link: neetcode.io/problems/implemen...
0:00 - Read the problem
1:53 - Drawing Explanation
11:40 - Coding Explanation
leetcode 208
This question was identified as an interview question from here: github.com/xizhengszhang/Leet...
#trie #prefix #python
Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.

Пікірлер: 158

  • @NeetCode
    @NeetCode3 жыл бұрын

    🚀 neetcode.io/ - I created a FREE site to make interview prep a lot easier, hope it helps! ❤

  • @evalyly9313
    @evalyly93132 жыл бұрын

    This is the best Trie introduction video I watched in youtube, very clear and concise. Bravo!

  • @sanjanar9198
    @sanjanar91982 жыл бұрын

    Never thought Trie would be this easy to understand, thanks a lott!!

  • @sucraloss
    @sucraloss9 ай бұрын

    You legitimately explain these concepts better than any professor I've had, and I went to one of the most highly regarded schools in the US for Computer Science. You outdid both the professors and their teaching assistants. You have a gift for explaining this stuff.

  • @z7847
    @z78472 жыл бұрын

    Neetcode really saving the day here. I watched other videos, but none explained and illustrated how to code it efficiently. Your drawing is so helpful and the clean, simple code makes sense. Thanks Neet!

  • @sprajosh
    @sprajosh Жыл бұрын

    I avoided trie for so long thinking it will be difficult to understand. Thanks for this. This was very easy.

  • @niravarora9887

    @niravarora9887

    5 ай бұрын

    I am with you on this, hahaha

  • @kritmok1875
    @kritmok1875 Жыл бұрын

    never thought that I would get the best trie introduction video in a leetcode video. Thanks for the great work!

  • @sravanikatasani6502
    @sravanikatasani65023 жыл бұрын

    Thank you so much!! I have learnt a new data structure today..

  • @reddev9143
    @reddev91433 жыл бұрын

    Thank you so much! Just can’t tell how cool and valuable your videos are! Keep’em coming!

  • @saumyatyagi8768
    @saumyatyagi8768 Жыл бұрын

    This is the best Trie vid so far ! Excellent explaination . 👍

  • @seanjcan
    @seanjcan11 ай бұрын

    Best explanation of Tries I have come across so far. Bravo!

  • @rishiraj3521
    @rishiraj3521 Жыл бұрын

    Always love the way you explain things so easily and concisely.

  • @Xavierrex3
    @Xavierrex36 ай бұрын

    Man your videos are incredible, and I've learned so much about programming in general just from your channel. Before this I was stunned about how to approach problems like this or dynamic programming but just from following solutions and understanding the concepts better I can do problems like this moderately well now

  • @janeikeliu
    @janeikeliu Жыл бұрын

    Thank you! Your explanation of of the trie data structure is so clear!

  • @rkkasotiya
    @rkkasotiya11 ай бұрын

    Simplest video to understand Trie and implement it. Keep up the good work.

  • @TejaDuggirala
    @TejaDuggirala2 жыл бұрын

    You are literally making my life easy. Thanks man 🙏🏻

  • @nhuphan7112
    @nhuphan71122 жыл бұрын

    honestly just want to say your videos been great in my interview prep :) (totoal newbie at leetcode)

  • @krutimody91
    @krutimody91 Жыл бұрын

    Omg this is amazing, I have been trying to understand this for an hour now - finally I get it! Thanks!

  • @vashishtgupta7416
    @vashishtgupta74162 жыл бұрын

    I haven't seen better tutorials than yours for LC problems anywhere! Great work :)

  • @farshadsaberi2740
    @farshadsaberi27402 жыл бұрын

    Thank you very much for clear explanation and truly neat code :)

  • @davi_paula
    @davi_paula Жыл бұрын

    Amazing explanation, great job!

  • @WalterGordyCanada
    @WalterGordyCanada9 ай бұрын

    These videos are literally better than the courses I took in University. Thank you!

  • @ambermichellemartinez
    @ambermichellemartinez Жыл бұрын

    Thank you, the best trie explanation! You're amazing!!!

  • @vivitra1022
    @vivitra1022 Жыл бұрын

    Thank you very much for the very clear explanation!!!

  • @noa.leshem
    @noa.leshem Жыл бұрын

    Super helpful videos. Been looking at them a lot.

  • @NeetCode

    @NeetCode

    Жыл бұрын

    Thank you so much 🙏

  • @lymmontijo87
    @lymmontijo873 ай бұрын

    Thank you!! I had never seen tries and had been avoiding it. Its actually quite easy.

  • @mruduladdipalli5417
    @mruduladdipalli5417 Жыл бұрын

    A Big Salute To You Bro, for the solutions and great explanation , I have recommended this channel to at least more than 3 people 🙌

  • @MP-ny3ep
    @MP-ny3ep Жыл бұрын

    You are a godsend. Thank you sooooooo much for all your work 🙏🙏

  • @osamaayman3765
    @osamaayman3765 Жыл бұрын

    I had no other option but to like this implementation! Thanks Kevin :)

  • @Michalr1223
    @Michalr12232 жыл бұрын

    Great explanation! thank you :)

  • @1-ov947
    @1-ov9479 ай бұрын

    You are the best leetcode teacher on entire KZread!

  • @amitrastogi1405
    @amitrastogi14057 ай бұрын

    Very well explained. Thanks!

  • @wajahatali6403
    @wajahatali64032 жыл бұрын

    Excellent video, as always.

  • @vishalrao5202
    @vishalrao52022 жыл бұрын

    Beautifully explained 👏🏻👏🏻👏🏻

  • @changlingao4609
    @changlingao4609Ай бұрын

    thank you so much! such an awesome explanation of Trie you make everything so easy to understand thanks!!

  • @carloslazarin
    @carloslazarin Жыл бұрын

    Excellent explanation!! thank you a lot!

  • @elias043011
    @elias043011Ай бұрын

    Great explanation! The very important take away here was that you mark the end of a valid word with the TrieNode variable, otherwise "appl" may look like a valid word after inserting "apple"

  • @codeUrDreams
    @codeUrDreams Жыл бұрын

    Brilliant! Thank you so much!

  • @MinhNguyen-lz1pg
    @MinhNguyen-lz1pg2 жыл бұрын

    Awesome explanation my man!

  • @tjsm4455
    @tjsm44557 ай бұрын

    Good explanation. After understanding the logic I implemented the code myself. Feels good.

  • @jasonahern3498
    @jasonahern34982 жыл бұрын

    Thank you! Great video

  • @madanmohanpachouly6135
    @madanmohanpachouly6135 Жыл бұрын

    Real cool explanation, Thanks Man.

  • @LongVu-sm8wm
    @LongVu-sm8wm2 жыл бұрын

    a great explanation !. Thanks

  • @iugaialeksei2108
    @iugaialeksei21087 ай бұрын

    OMG, man, you possess a special talent to explain complicated things smoothly and simple. I'm a big fan of your channel!

  • @YunChiehChiu
    @YunChiehChiu Жыл бұрын

    Thank you so much. Your videos makes everyday learning a feasible plan. Your explanation makes every single problem lots more easier to understand and reduce my imagination to the difficulty of the problem.

  • @sudharahul2010
    @sudharahul20105 ай бұрын

    Great practical explanation of Trie!!

  • @saienrique
    @saienrique2 жыл бұрын

    Dude i have been postponing this DS for an year thinking its going to be very tuff. U just confirmed it to be wrong in first ten mnts. Great job man!

  • @killeraloo3247
    @killeraloo32472 ай бұрын

    Loved the easy to understand explanation. 🧡 from remote.

  • @BobbyMully
    @BobbyMully2 жыл бұрын

    When I did this one before wathcing the video, my code was double the length of yours. Nice solve!

  • @unanimous8510
    @unanimous85109 ай бұрын

    What an amazing explanation for the guy who just started learning tries 👍

  • @wallaby28
    @wallaby283 жыл бұрын

    Brilliant explanation

  • @ashs3979
    @ashs39792 жыл бұрын

    You’re awesome dude ty

  • @umairbijapure7584
    @umairbijapure75842 жыл бұрын

    Amazing explanation

  • @emmatime2016
    @emmatime20162 жыл бұрын

    You are always the best!

  • @Dhruvbala
    @Dhruvbala18 күн бұрын

    This was one of the more enjoyable problems I've done

  • @bhabishyachaudhary3495
    @bhabishyachaudhary34956 ай бұрын

    really helpful thank you so much.

  • @Manu-et6zk
    @Manu-et6zk3 жыл бұрын

    please explain time and space complexity after u solve the problem

  • @juliacheng4751

    @juliacheng4751

    2 жыл бұрын

    Time is O(n*m) for insertion (n is the number of words, m is the max len), O(m) for search. Space is O(n * m)

  • @8nehe
    @8nehe2 жыл бұрын

    Great simple explanation

  • @aaab45357
    @aaab45357Ай бұрын

    A note on implementation: instead of a python class representing the node, you can simply represent each node as a dictionary of chars to dictionaries and use a placeholder (like '$') to mark the end of a word. This will be more efficient (same big-O, though) in python in both memory and time, for you are not wasting a bunch of space pre-allocating lists, you are not having to deal with python's overheads and, most importantly, you would be using a highly optimized python data structure calling c under the hood. On top of all of that, you get a cleaner implementation

  • @dorondavid4698
    @dorondavid46982 жыл бұрын

    Nice explanation!

  • @barcannon
    @barcannon2 жыл бұрын

    Great explanation

  • @ameynaik1755
    @ameynaik17553 жыл бұрын

    Neat video! Please do a video on Design Search Autocomplete System.

  • @tarushgupta6616
    @tarushgupta66168 ай бұрын

    Excellent explanation

  • @RobinHistoryMystery
    @RobinHistoryMystery3 ай бұрын

    thanks! I thought Trie is gonna be super hard, nice explanation

  • @ruzaikr
    @ruzaikr4 ай бұрын

    Thanks!

  • @elachichai
    @elachichai2 жыл бұрын

    crisp and clear

  • @antonyndungu5514
    @antonyndungu55142 жыл бұрын

    I have watched a few explanations on the tries, this is the best. Keep them coming at least for now you don't have a job

  • @TechOnScreen

    @TechOnScreen

    2 жыл бұрын

    dude he is in google !.. someone truly said. Time is mightier than fate..

  • @antonyndungu5514

    @antonyndungu5514

    2 жыл бұрын

    @@TechOnScreen Yah, I knew with his level of skills its just a matter of time and he will get one of those top jobs in the industry

  • @TechOnScreen

    @TechOnScreen

    2 жыл бұрын

    @@antonyndungu5514 yeah dude.. hope some day we will have our luck too 😂

  • @antonyndungu5514

    @antonyndungu5514

    2 жыл бұрын

    @@TechOnScreen True, can't wait for that :-)

  • @lingyundai964
    @lingyundai96411 ай бұрын

    brilliant!

  • @hmanjun7260
    @hmanjun7260 Жыл бұрын

    Using a hash map was genius. Once I saw that, I went and redid my code before looking at the rest of ur solution

  • @nou4605
    @nou46054 ай бұрын

    The code is marvelously simple considering how useful of a data structure this can be.

  • @DeepakKumar-wu4dt
    @DeepakKumar-wu4dt2 жыл бұрын

    Thanks Man!

  • @murtuza.chawala
    @murtuza.chawala10 ай бұрын

    The best Trie Explanation

  • @arnabpersonal6729
    @arnabpersonal67292 жыл бұрын

    neatly implemented

  • @ariansergi7929
    @ariansergi79292 жыл бұрын

    good job

  • @hoyinli7462
    @hoyinli74622 жыл бұрын

    great video!

  • @MichaelShingo
    @MichaelShingo Жыл бұрын

    yess!! tries are cool

  • @palashbehra9303
    @palashbehra93038 ай бұрын

    Awesome!

  • @sundararajannarasiman5613
    @sundararajannarasiman56132 жыл бұрын

    Very helpful

  • @hwang1607
    @hwang16076 ай бұрын

    helpful video

  • @princeanthony8525
    @princeanthony8525 Жыл бұрын

    Can you code up the delete functionality as well ?

  • @diplodocus3
    @diplodocus35 ай бұрын

    skimmed through 3-4 blah blah videos, this finished the job gracefully

  • @symbol767
    @symbol7672 жыл бұрын

    My favorite data structure, I just came to make sure my code was as good as the one you show, and its exactly the same thankfully. Thanks for your videos Liked!

  • @KrzysztofKoziarski
    @KrzysztofKoziarski Жыл бұрын

    Thanks

  • @noctua7771
    @noctua7771 Жыл бұрын

    After your explanation I was able to code it in under 2 mins without looking back at the video. You sir are an absolute legend.

  • @deniyii
    @deniyii9 ай бұрын

    A more useful and challenging problem would be to return a list of words that starts with a given prefix. Pretty much autocomplete. I’m not sure if that one can be solved iteratively though, I’ve only seen it done recursively.

  • @raghavendharreddydarpally2125
    @raghavendharreddydarpally21253 жыл бұрын

    Keep doing more videos

  • @harrywang9375
    @harrywang93752 жыл бұрын

    It should be mentioned that marking Apple as the end of the word is not just to be safe. Your function would return false for not found if your word search was searching for both Apple and Apples if you didn't have that boolean to check if it was the end of the word. If Apples existed for example, then the E in Apple would no longer be the end of the chain. Despite that fact, Apple is a very legitimate word even though it has a child node in 'S'.

  • @mohammedfahadnyc1385

    @mohammedfahadnyc1385

    Жыл бұрын

    when you enter apple, it will mark e as the end of the word, then again when u enter apples, it will run for the length of apples, and add s after e. Then if u search, u will get True for both. Just because a node is marked end = True doesnt mean u cant access or add to that Node.

  • @harishkandan7910
    @harishkandan79102 жыл бұрын

    Hey, what if we insert a substring of an already existing string. Say we insert 'app' when 'apple' already exists in the Trie. Wouldn't that make the second 'p' in the 'apple' also an 'endOfWord' making the 'le' in 'apple' inaccessible?

  • @kamaleshs5324

    @kamaleshs5324

    2 жыл бұрын

    No harish, because you will only be checking for the endOfWord condition when you have reached the end of the particular word you are searching for, say you are searching for apple and both the second p and the e are marked as endOfWord, then when you are searching for apple, you iterate one by one, a, p, p, l, e and after reaching the end point, only then you check for the endOfWord Condition. In case you are searching for app, then you iterate through a, p, p and then check whether the currentNode has endOfWord as true, in this case yes, so both app and apple are accessible.

  • @DeGoya

    @DeGoya

    2 жыл бұрын

    @@kamaleshs5324 that doesn't answer the question though, since he asked what will happen if "we insert a substring of an already existing string"

  • @torin755

    @torin755

    2 жыл бұрын

    No, it will not be inaccessible, simply because if you look again at the search function you will see the for loop runs down the tree for the length of the searched word REGARDLESS of if it finds a node with EndOfWord. Only AFTER it has gone down the length of the word, will it check if the node it LANDS on is EndOfWord=True. I.e it will NOT stop just because it found an EndOfWord, but instead traverse the word's chars entirely and then check if the node it lands on is EndOfWord=True

  • @janvidalal476
    @janvidalal476 Жыл бұрын

    this guy is making our life play on easy mode

  • @Lmcrf
    @Lmcrf11 ай бұрын

    you don't need to create additional TrieNode class, just initialize everything in TrieNode class into Trie class. and replace every cur = self.root by cur = self and it works fine i don't know why, this new solution was much faster, may be it was leetcode bug

  • @smartsoothing776
    @smartsoothing7765 ай бұрын

    If you could have explained all the data structures like this before, My life would have become easier..

  • @qR7pK9sJ2t

    @qR7pK9sJ2t

    5 ай бұрын

    But did you come earlier to get his help is the question that I want to ask from you ?

  • @Joseph-ub7rh
    @Joseph-ub7rh2 жыл бұрын

    My assumption for runtime complexity is O(n) where n is the number of characters in the string that we are inserting or searching for. Now for space complexity, it seems to me that it is O(n^n) because each node can have n children.

  • @hillarioushollywood4267

    @hillarioushollywood4267

    Жыл бұрын

    not n nodes can have n children, you can say, 26 chars can have n children :) That Means, O(n+m+k+.........26th l)

  • @hamoodhabibi7026

    @hamoodhabibi7026

    Жыл бұрын

    @@hillarioushollywood4267 So this is Space: O(n * 26) where n is the length of our word and each character in our word can have 26 children max

  • @yuenyiupang
    @yuenyiupang2 жыл бұрын

    Very good explanation, but can someone tell me which way is more efficiency(if we dont care space) on above case, Trie, or a hashmap include all combination app become a = [app], ap = [app], app = [app], and 1 more hash for just store the word, as latter way can ensure search and startwith run O(1) time?

  • @sirmidor

    @sirmidor

    Жыл бұрын

    Yes, you can achieve average O(1) for searching and startsWith this way, though your insert will be slower. For a word of length m, I believe it'll be be O(m^2): you'll be adding m strings, obtaining those through list slicing, which is O(slice length). Average slice length is about half the length of the word in this case (for a word of length 4: 1st prefix is length 1, second is 2, third is 3, last is 4), so m * m * ~0.5, which is O(m^2). Doing it this way could be better if you inserted rarely, but searched and used startsWith a lot. You said not to care about space, but this is more space-efficient as well as a hashset is a built-in optimized data structure, while a TrieNode is an entire user-defined class instance.

  • @travellercoder7298
    @travellercoder72982 жыл бұрын

    Sir i have a doubt that in apple it's two p's and since we inserted the first so will it insert the second one?? Plzz explain

  • @atharhussain6534

    @atharhussain6534

    Жыл бұрын

    Data structure look like for letter apple : {"a":{"p":{"p":{"l":{"e:{}}}}}

  • @4498rx
    @4498rx Жыл бұрын

    you are the best

  • @yakeensabha655
    @yakeensabha655 Жыл бұрын

    what's the complexity of search, insert, delete the word in the trie? when it's implemented using a 26 array? I'm tryna guess it? the search and insert is O(L which is the length of the word)? the all space is 26 * the number of all nodes? plz I need to know

  • @illu1na
    @illu1na8 ай бұрын

    Do we have to use trieNode? I used dictionary and had "None" to mark if the node is at the end.

  • @samihk1
    @samihk12 жыл бұрын

    This is the best Data Structure, I can think of 100s of things I can do with Trie. Well explained, I can say the speed at which you teach is on-target, I never fast-track your videos.

  • @shadowsw8020
    @shadowsw802019 күн бұрын

    I like this one

  • @b9944236
    @b9944236 Жыл бұрын

    Perfect

Келесі