L1. Implement TRIE | INSERT | SEARCH | STARTSWITH

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

КОМЕНТАРІ • 408

  • @takeUforward
    @takeUforward  3 роки тому +207

    Just a small request, please do comment “understood” if you understand by watching this video. Helps me a lot mentally. Hardly will take 2 seconds!!
    Lets start this trend on TUF for every video you watch ☺️

    • @agrawalhimanshu
      @agrawalhimanshu 3 роки тому +2

      IN thumbnail it should be implement TRIE instead of implement TREE

    • @hareeshr3979
      @hareeshr3979 3 роки тому +2

      I completed the SDE sheet still imposter syndrome is killing me 😢, I'm at my 5th semester

    • @noobprogrammer3040
      @noobprogrammer3040 2 роки тому

      bhaiya your explanation is the best.

    • @swapnilsingh710
      @swapnilsingh710 2 роки тому

      understood

    • @aeroabrar_31
      @aeroabrar_31 Рік тому +3

      Thanks s'TRIE'ver for this amazing 'TRIE' playlist.. 😅😁📌

  • @vikrambabariya5166
    @vikrambabariya5166 2 роки тому +42

    Completed Trie Series, I can definitely say your way of teaching is best in the teaching field. Thanks for providing this type of highly rated content free of cost. 🙏

  • @shree_divyansh
    @shree_divyansh 3 роки тому +62

    Understood!!! You explained so good that after watching the insert explaination of trie (till 13:23), I paused the video and coded insert, search and startsWith. After that I resumed video to match the code.

  • @prateekgaur2709
    @prateekgaur2709 2 роки тому +4

    At 29:30 ,in startsWith func(),for loop should be from i=0 to i

  • @dharani8871
    @dharani8871 Рік тому +6

    Understood. No one can do a dry run like you do. You are perfect.

  • @Ishan301
    @Ishan301 2 роки тому +146

    Small Correction at 24:57 in C++ code line 42, when we will move to the reference trie it should be node=node->get(word[i]); instead of just node->get(word[i]); By the way great content and really nice explanation :)

    • @sukritiguin5637
      @sukritiguin5637 2 роки тому +8

      Yes.. current node is updating every times

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

      Yes Exactly I was also wondering the same.

    • @rohitk472
      @rohitk472 Рік тому +3

      same I was thinking.

    • @K.AnshulReddy
      @K.AnshulReddy 4 місяці тому +4

      bro u have literally saved my day ,i was really confused how this was working

  • @evergreen7781
    @evergreen7781 3 роки тому +4

    Not only did I understand, but also revised & learned best practices using OOPs.

  • @tanujajoshi5253
    @tanujajoshi5253 3 роки тому +7

    This video is really good. I was actually looking for a Trie playlist. I started trie a week back from gfg but couldn't understand. Now I'm starting again from your playlist. Please continue making videos for this playlist so that I can complete this concept timely. Thank you. Lots of support. And yeah, "understood".

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

    Thank you so much. I finally learnt Trie. After your explaination and seeing the struct Node. I decided to give it a try and code on my own. In

  • @likhithl7928
    @likhithl7928 2 роки тому +4

    I was always demotivated by the complexity in Tries.. For the very first time i understood everything.. I felt very high after being able to code it in python myself.... Thanks man ❤

  • @bharathjanakiraman7232
    @bharathjanakiraman7232 2 роки тому +3

    Understood. Also, I was able to solve the shortest unique prefix problem by only watching this video. Great explanation!!. Thanks a lot for this!

  • @stith_pragya
    @stith_pragya 10 місяців тому +1

    UNDERSTOOD........Thank You So Much for this wonderful video.............👍👍🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @lakshaysharma6550
    @lakshaysharma6550 2 роки тому +18

    Here is the python code for my fellow pythonistas:-
    class Node():
    def __init__(self) -> None:
    self.links=[None for _ in range(26)]
    self.flag=False
    def containsKey(self,ch):
    return self.links[ord(ch)-ord('a')]!=None

    def get(self,ch):
    return self.links[ord(ch)-ord('a')]
    def put(self,ch,node):
    self.links[ord(ch)-ord('a')]=node

    def setEnd(self):
    self.flag=True

    def isEnd(self):
    return self.flag

    class Trie():
    def __init__(self):
    self.root=Node()

    def insert(self,word):
    node=self.root
    for i in word:
    if(not node.containsKey(i)):
    node.put(i,Node())
    node=node.get(i)
    node.setEnd()
    def search(self,word):
    node=self.root
    for i in word:
    if(not node.containsKey(i)):
    return False
    node=node.get(i)

    return node.isEnd()

    def startsWith(self,prefix):
    node=self.root
    for i in prefix:
    if(not node.containsKey(i)):
    return False
    node=node.get(i)
    return True
    if __name__=='__main__':
    type=[1,1,2,3,2]
    value=['hello','help','help','hel','hel']
    trie=Trie()
    for i in range(len(type)):
    if(type[i]==1):
    trie.insert(value[i])
    elif(type[i]==2):
    print(trie.search(value[i]))
    else:
    print(trie.startsWith(value[i]))

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

    Long lasting trie-phobia has been recovered. Thanks for the content.

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

    love you dada from Howrah, West Bengal. You are my inspiration.... :)

  • @AbhishekSingh-oz5ei
    @AbhishekSingh-oz5ei Рік тому +3

    Just a small correction in C++ code line 42, when we will move to the reference trie it should be node=node->get(word[i]); instead of just node->get(word[i]); And 2nd one is line 13, return type is void instead of bool. Btw amazing video to understand trie in better way.

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

      i was also thinking the same

    • @hardikkumarsingh
      @hardikkumarsingh 10 місяців тому +1

      There is no error in line 13 , I mean return type would be bool only not void. I guess you meant in line 23, it should be void

  • @AliAkbarFaiz
    @AliAkbarFaiz 2 роки тому

    Understood? Was able to code in a single attempt without looking anywhere else. That's how simple u made it 👍❤️

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

    He was right when he said He'll not be running the code as he might have done some syntax errors, and he really has, at 24:57 in C++ code line 42, it will be node=node->get(word[i]); instead of just node->get(word[i]). Man is even ahead of himself.

  • @AB-tp9eg
    @AB-tp9eg 3 роки тому +38

    Understood, bhaiya one thing that I want to request that please keep your face in every video while teaching. It would be great 🙂

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

    UNDERSTOOD Striver :) Thank you so much for all your efforts! Truly appreciable!

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

    So far the best Trie Data Structure video on youtube

  • @AhmadSayeed-plus
    @AhmadSayeed-plus Рік тому

    Understood bro. first video in life to understand trie and learned it in first video. What a luck for me

  • @atharvakulkarni2024
    @atharvakulkarni2024 2 роки тому +8

    A FEEDBACK CODING LIVE IS MUCH MUCH BETTER THAN EXPLAINING THE WRITTEN SOLUTION..PLEASE FOLLOW THE SAME METHOD FOR FURTHER SERIES!!!1THANKS :)

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

    Starting this trie series. Always wanted to study about tries because I am unable to solve hard questions requiring it's concepts. Hopefully I get clarity with this series. Completed tree series first before starting this!

  • @KapilMaan-vw9sd
    @KapilMaan-vw9sd 12 днів тому

    understood sir ji
    very nice video sir
    your hardwork is amazing !!!

  • @madhavaprashath1453
    @madhavaprashath1453 2 роки тому +3

    What happens when we try to insert a string that is a prefix of previously inserted string???

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

    Its really so nice..Understood each and everything..

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

    Understood! Thanks a lot for such a detailed and understandable tutorial!

  • @bhaveshkumar6842
    @bhaveshkumar6842 2 роки тому +2

    Google is lucky to have you 😎😎😎

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

    Understood! reallly a great explaination able to understand in one shot!

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

    why the return type of setEnd() is bool, shouldn't it be void if it isn't returning anything

  • @UECAshutoshKumar
    @UECAshutoshKumar 7 місяців тому +1

    Thank you 😊

  • @zahidgul6337
    @zahidgul6337 9 місяців тому

    in search function, it should be prefix.length instead of word

  • @akshaywairagade5478
    @akshaywairagade5478 2 роки тому +5

    I have a doubt in insertion part consider a word "app" if we first insert it then p will point to null and later if we insert "apple" now p is pointing towards 'l' so here we have lost the fact that "app" word also exists as by our word we should get a null node. So how to deal with this problem @take U forward @Striver

    • @utkarshsharma1185
      @utkarshsharma1185 2 роки тому +2

      when we done traversing the word "app" we will check for the flag == true if yes then word exist otherwise not.

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

      @akshay
      Actually, I was also thinking about the same problem
      The first time when "app" will be inserted, the second p will point to a null trie and that flag will be marked as true
      Later when "apple" is inserted then it would look like this
      ('a', false)->('p', false)->('p', false)->('l'-> *true*)->('e'->false)->(NULL->true)
      So after you search for "app", it will still return true

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

    people would say that trie is a hard topic, but with you, it does not seem so

  • @TheElevatedGuy
    @TheElevatedGuy 2 роки тому

    Understood! Really Appreciate it!
    thanks a lot!
    Keep making these type of content for us!
    Thank you very much it is very much helpful.

  • @MayurNagrale-py7lu
    @MayurNagrale-py7lu Рік тому

    node = node->get(word[i]) in insert method

  • @sunnykumarsingh7039
    @sunnykumarsingh7039 3 роки тому +1

    Just woke up.
    This video is go-with-my-breakfast video❤️

  • @parambharti7095
    @parambharti7095 2 роки тому +1

    Just Awesome. Thanks a lot Striver.

  • @utkarshmishra6667
    @utkarshmishra6667 3 роки тому +2

    Understood...btw striver you have a great teaching proficiency my friend...as amazing as it can be...🔥🔥✌

  • @sommayghosh4617
    @sommayghosh4617 2 роки тому

    Striver hai toh Trie ko try krskte hai, only DS skipped will try to do now!, Great and lucid explanation ,cheers 🍻🍻

  • @s.g.prajapati3597
    @s.g.prajapati3597 3 роки тому +1

    Awesome Explanation!
    UNDERSTOOD TO THE FULLEST!

  • @areebahmadsiddiqui2648
    @areebahmadsiddiqui2648 3 роки тому +2

    Understood excited for the upcoming videos thanq Striver🤩🤩

  • @mohaktrivedi9591
    @mohaktrivedi9591 2 роки тому

    Understood in one go! Thanks for the concise explanation!

  • @jaykumargupta7307
    @jaykumargupta7307 2 роки тому

    Leetcode daily challenge forced me to come here.. Happy I did!!!

  • @masterroyjones4228
    @masterroyjones4228 3 роки тому

    Explaining complicated ds like a pro is jst a striver bhaiyya thing nobody else can do that💯

  • @yashsinha4712
    @yashsinha4712 3 роки тому +1

    Understood !!!!!
    Amazing explanation ,it was so easy

  • @eshandhok2591
    @eshandhok2591 2 роки тому +1

    What is the prerequisite for Trie? also, before or after DP? @anyone and @everyone

  • @aditidey9366
    @aditidey9366 2 роки тому +1

    understood! quick question though, can't we use a char to int map instead of the array?

  • @SriHarshaChilakapati
    @SriHarshaChilakapati 3 роки тому

    Fantastic explanation! Just two suggestions:
    1. For Striver: Small letters t and f are looking identical in your writing, so consider writing T or F in capitals for added clarity.
    2. For Viewers: This is a good way when you are guaranteed that the strings given are between a to z and in small case. If you have to generify it for all characters, how would you change it? Think about this question a little.
    :)

    • @takeUforward
      @takeUforward  3 роки тому +2

      Sure Harsha, my handwriting is super bad 🤪

    • @SriHarshaChilakapati
      @SriHarshaChilakapati 3 роки тому +1

      @@takeUforward Hey, no. Don't take it in a negative sense. Except for that one instance, your handwriting is pretty much fine.

    • @SriHarshaChilakapati
      @SriHarshaChilakapati 3 роки тому +1

      @@takeUforward Btw, which Whiteboarding tool are you using?

  • @monismomin6759
    @monismomin6759 Місяць тому

    Understood!!! , great explaination

  • @rohitantil9534
    @rohitantil9534 2 роки тому

    Bhai kmal he krdea hitna difficult lgta tha ye topic Lekin abb bhot easy❤️

  • @yuvi3398
    @yuvi3398 2 роки тому +4

    Hey striver I am able to understand trie concepts taught by you but I am having difficulty in understanding Object oriented stuff in C++ that you are telling in the video. Could you please recommend some resource from where we can learn this OOPS stuff conceptually or the resource from you learned and mastered it.

  • @accessdeniedk6240
    @accessdeniedk6240 2 роки тому

    Such a great video! Easily understood and thanks for providing us with such contents.

  • @keshav2113
    @keshav2113 3 роки тому +10

    Wall paper pe correct kar lo bhaiya Implement tree dikha raha hai !!

  • @_SOHAMSAMANTA
    @_SOHAMSAMANTA 2 роки тому

    Awesome Explanation !!! And thanks for explaining the production level code .

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

    compleately understood sir you are amazing

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

    links could be taken as a map , It would reduce the complexity of containsKey class function a little bit.

  • @Niranjan-dt2mc
    @Niranjan-dt2mc 3 роки тому +2

    I can not do anything for you but....Just thank you so much Bhaiya ❤️... Learnt a lot form you.... Inspiration to work hard 🙏

  • @therealsumitshah
    @therealsumitshah 2 роки тому

    understood striver...you are a gem.

  • @hasansyed1004
    @hasansyed1004 2 роки тому +3

    void insert(struct TrieNode *root, string key)
    {
    TrieNode *node = root;
    for(int i=0;ichildren[key[i] - 'a']==NULL)
    {
    node->children[key[i] - 'a']= new TrieNode;
    }
    node=node->children[key[i] - 'a'];
    }
    node->isLeaf = true;
    }
    //Function to use TRIE data structure and search the given string.
    bool search(struct TrieNode *root, string key)
    {
    TrieNode *node = root;
    for(int i=0;ichildren[key[i] - 'a']==NULL)
    {
    return false;
    }
    node = node->children[key[i] - 'a'];
    }
    return node->isLeaf;
    }

  • @champion5946
    @champion5946 2 роки тому

    UnderStood
    You said production level code , So
    Can we use range based for loops whenever index is not required ?

  • @rishabhkumar8115
    @rishabhkumar8115 2 роки тому

    Thanks a lot for this valuable things a for free.

  • @akshatsahu8021
    @akshatsahu8021 3 роки тому

    That was cool... got the concept and implementation pretty clearly.. All thanks to you bro.

  • @LevelUpJourney-d2d
    @LevelUpJourney-d2d 2 роки тому

    Understood !!! sure man thank you so much for sharing keep pushing that content

  • @akashkataria3218
    @akashkataria3218 7 місяців тому +1

    Why can't we directly use Trie class to represent Nodes? What is the benefit of using different class to represent Trie Nodes?

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

      hey! akash striver ne directly questions karwayein h ya phele trie ke baare mei btaya bhi h

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

    Understood.
    Which notepad are you using for writing ?

  • @itsyadavraj
    @itsyadavraj 3 роки тому +2

    amazing lecture sir😍
    thank you so much 🙏🙏

  • @Manojkumar-rq8hf
    @Manojkumar-rq8hf 2 роки тому

    Great Explanation! Understood very well.

  • @devendrapatil07
    @devendrapatil07 2 роки тому +1

    Default Value of all element of array is pointer is NULL ??

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

    teaches well understood well😊

  • @sanbidrc
    @sanbidrc 3 роки тому +1

    Great content as always. Btw How to insert "apples" AFTER inserting "apple". Wouldn't "apple" be lost?

    • @takeUforward
      @takeUforward  3 роки тому +9

      No e’s reference trie has true marked, so u add a new trie for s to e’s reference trie and for s’s reference trie mark it as true

  • @manishsah7755
    @manishsah7755 9 місяців тому

    Can some explain me the time complexity of insertion-
    I have a doubt in insertion we are creating a pointer array of size 26 so why time complexity is o(n) it should be o(26*n)?

  • @abhishekgupta9705
    @abhishekgupta9705 2 роки тому

    Understood. Hats Of!!!!!!!!!!!!!!1

  • @MazharulIslam-f9v
    @MazharulIslam-f9v Рік тому

    Understood... super explanation...

  • @UnknownUnknown-sw4hb
    @UnknownUnknown-sw4hb Рік тому

    Understood striver Thank you

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

    Nice and clear explanation! Just one question. Is there any specific reason for using a struct for Node instead of using a class like the way it has used in the Java code?

  • @mallikdasari
    @mallikdasari 2 роки тому

    Great explanation and coding with industry standards. This will help not only in the interview but also in the daily work.

  • @shatakshisharma8557
    @shatakshisharma8557 2 місяці тому

    if 'ba" exists then why not 'appl"? in that case after "l" we will be at not null position as well.

  • @gandhijainamgunvantkumar6783
    @gandhijainamgunvantkumar6783 2 роки тому

    Understood bhaiya. Keep making this type of amzing content :)

  • @harshverma1221
    @harshverma1221 3 роки тому +1

    For all those complaining for the background
    Striver also noticed that hence placed his photo just over that e

  • @laraibanwar1618
    @laraibanwar1618 3 роки тому

    Brilliant
    Please make a video of suffix array which jnvloves nlogn solution
    Thnks

  • @vibhaalur4078
    @vibhaalur4078 3 роки тому

    Understood ! Very well explained !!

  • @TechieDishant
    @TechieDishant 3 роки тому

    Awesome Explanation! 🔥
    UNDERSTOOD

  • @BEEMohdArbazAli
    @BEEMohdArbazAli 3 роки тому

    Very Well understood. Thanks bhayya

  • @samyakjain7422
    @samyakjain7422 2 роки тому

    Main tera haaye re jabra
    Hoye re jabra fan ho gaya!

  • @atharvakulkarni2024
    @atharvakulkarni2024 2 роки тому

    Why do we need an extra struct can't we do it using a class Only?

  • @ManishKumar-qs8xh
    @ManishKumar-qs8xh 5 місяців тому

    You are the best, man!

  • @harshverma1221
    @harshverma1221 3 роки тому +5

    Understood, very nice lecture 😉

  • @siddharthsaxena6495
    @siddharthsaxena6495 2 роки тому

    Can u tell the space complexity of trie , because we are taking the links array again and again for reference trie??

  • @bhaveshkumar6842
    @bhaveshkumar6842 2 роки тому +1

    Striver bhai, you're the best!

  • @manideepdeepnaidugorle7308
    @manideepdeepnaidugorle7308 4 місяці тому +1

    understood !!!

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

    Understood. Thanks, Raj!

  • @uchihaPrincessSakura
    @uchihaPrincessSakura 3 роки тому +1

    Watched it completly Raj Sir

  • @sks7856
    @sks7856 2 роки тому +1

    bro, you are doing great work, learnt a lot from your videos....thanks for providing such a quality content.

  • @subhadipmaity3253
    @subhadipmaity3253 2 роки тому +1

    One doubt: How to insert "apps" and "app" in a trie ?? or "apples" and "apple" ??, please reply

  • @aarushirai6374
    @aarushirai6374 2 роки тому

    Understood. Really helpful.

  • @_-6912
    @_-6912 2 роки тому

    understood Striver. Thank you.

  • @subhroacharjee1562
    @subhroacharjee1562 2 роки тому

    best teacher ever

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

    what if we have a word and its prefix as another word how are wo gonna handle that case as last char will not point to blank so we will conclude that this word is not present but is is present actually