Data Structures: Solve 'Contacts' Using Tries

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

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

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

    I watched lots of your video, you're the best who explains a problem clearly with easy understandable code.

  • @lg2389
    @lg2389 7 ปีที่แล้ว +11

    Why would you increment the size at the beginning of the add method? Shouldn't you increment the size once you set a node?

    • @chiyang4776
      @chiyang4776 7 ปีที่แล้ว +4

      bcuz then size would equal number of words stored as in the graph on the right side. it only size++ at child = new Node(), then size will increment only at where it branches out.

    • @frankhou5574
      @frankhou5574 7 ปีที่แล้ว +2

      size stands for the size of the node itself, not children's size.

    • @ultimatesin3544
      @ultimatesin3544 7 ปีที่แล้ว +3

      It's a funny place for it and confused me for a while - but it does work - why? - think about what happens on the final node whenever you're adding a new string... If you were adding "ANDY", then at the end of the recursive method call - you'd be entering add() with node Y and with index = s.length... so if you don't increment size before returning, size for node Y would be zero...

  • @gadiben-amram1471
    @gadiben-amram1471 7 ปีที่แล้ว +7

    question - at the add() function, shouldn't we check if the contact we try to add already in the trie tree? if so, the "size++" is wrong since there will be no adding to the number of contacts on that string route... what am I missing?

    • @rohishd
      @rohishd 7 ปีที่แล้ว +1

      Yes, that is what I thought as well.

    • @rohishd
      @rohishd 7 ปีที่แล้ว

      I generally like her solutions but this solution is not really that great.

    • @dmitryWeirdo
      @dmitryWeirdo 7 ปีที่แล้ว +9

      Yes we have, but the description of this HackerRank task implies that there are no duplicate words in the input.

  • @galfas09
    @galfas09 7 ปีที่แล้ว +21

    The variable CharCode is not being used. By the way thanks a lot for your videos they are great and helping me a lot. =D

  • @tomyang7788
    @tomyang7788 7 ปีที่แล้ว +3

    what is the getCharIndex(char c) function for and why does it return c - a ?

    • @mnsayeed999
      @mnsayeed999 7 ปีที่แล้ว +10

      It is just to get the index of a character in the children[ ] array. children[ ] array is of size 26 (number of characters 'a' to 'z'). Java works on unicode characters concept, and since 'a' has an ASCII value of 65 (return type of getCharIndex(char c) is int, so it automatically converts character to its ascii value) and we have to place it at children[0.....25], so we do, ascii value of(c) - ascii value of('a').

    • @vishnusingh4118
      @vishnusingh4118 3 ปีที่แล้ว

      @@mnsayeed999 I think you meant 'a' has an ASCII value of 97. Because 65 is for 'A'

  • @AbhishekSingh-op2tr
    @AbhishekSingh-op2tr 7 ปีที่แล้ว +1

    At 2.24, for children instance variable's data type, she said though HashMap would take more memory but they(HM) would make certain operations easier. What does she mean by that? Wouldn't taking an array of length 26 every time is inefficient, since a node would not always have all 26 characters as children?

    • @dovgoldberg
      @dovgoldberg 7 ปีที่แล้ว

      An array is a collection of the same data types that are indexed. So with an an array of Ints you have 32bits stored in each item. With a hashmap there is a set of keys and values. So to store a hashmap of Integers would require the 32 bits per int plus the bytes used to store the key.

  • @doktoren99
    @doktoren99 7 ปีที่แล้ว +4

    If anyone managed to implement this in Swift let me know. Id love to take a look at it. Ive tried dictionaries with already implemented words for quick lookup .etc. but im still timing out on some of the tests

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

    I don't think this will find the count of contacts that exist containing that substring or start with that substring

    • @justynastaron-kajkowska3906
      @justynastaron-kajkowska3906 6 ปีที่แล้ว +2

      If surely does, if every name, that you have added, has all letters different and different letters then any other name, because then all nodes have size 1 (or doesn't exist) and therefore also last node have size 1, which is returned (or 0 is returned, if letter from name, that you are looking for, was never added). Handling duplicates is not solved in the add method, so find method also ignores it and doesn't work, as you would expect in the final solution, for the same reason - children doesn't track order of the characters.

  • @gravity_vi
    @gravity_vi 6 ปีที่แล้ว +1

    isn't it wrong you need to return the count of contacts that start with the string 's' and not all the strings which have prefixes as 's'.

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

    when I am a client who is using this class, from where should I get the index parameter while invoking findCount method?shouldn`t I just pass my "prefix" string and get the number of words corresponding to it?I think I don`t have to care bout something besides my prefix

  • @nikhilpatil611
    @nikhilpatil611 6 ปีที่แล้ว

    This logic would not work with duplicates, correct? A Trie will not have duplicates but by this logic size still gets incremented. Am I missing something?

    • @mayankgupta2543
      @mayankgupta2543 6 ปีที่แล้ว

      Nope i think you are right, the better place to increment size would be after setNode() call. As it ensures that a new node is added, so now increment the size. What say?

  • @mangono8
    @mangono8 7 ปีที่แล้ว

    why did she want the value at 5:35?

    • @joshgrey9251
      @joshgrey9251 7 ปีที่แล้ว

      if getNode(current) returned null the Node child would still be equal to null, when you try call child.add(...) it would just throw errors

  • @SulavTimsina
    @SulavTimsina 7 ปีที่แล้ว

    How does the getCharIndex() method work?

  • @sukumarkuppusamy4218
    @sukumarkuppusamy4218 6 ปีที่แล้ว

    findcount() is still confusing. Anybody please help with clear content.

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

      She didn't update size before calling the findCount method recur.

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

    There is minor bug in the code. The add method will set the size to one plius the word length because of "if (index == s.length() ) return; " in the add() method. It should be if (index == s.length() -1) return;

  • @SuyashSoni248
    @SuyashSoni248 7 ปีที่แล้ว +1

    What if I want to delete a contact from my contacts' directory? I've implemented deletion also - github.com/suyash248/ds_algo/blob/master/Trie/contacts_list.py

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

    Thank you Gayle

  • @programming_and_snacks5192
    @programming_and_snacks5192 6 ปีที่แล้ว

    I didn't understand code at all. Can someone explain it using example - "bat"

  • @programming_and_snacks5192
    @programming_and_snacks5192 6 ปีที่แล้ว

    Code would be clear if she actually walked through an example like "bat". Can someone walk through the code using this example?

  • @TheOwlTheOne
    @TheOwlTheOne 3 ปีที่แล้ว

    Her lectures are so hard to understand because her voice is so attractive i only listen to her but not understand anything😂

  • @MaminaZvezdochka
    @MaminaZvezdochka 3 ปีที่แล้ว

    Thanks so much!

  • @davidwoosley
    @davidwoosley 5 ปีที่แล้ว

    Is there any practical application where this solution is, in fact, the best solution? I simply cannot think of one.

    • @deadismarth7
      @deadismarth7 5 ปีที่แล้ว

      I phone contacts idk

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

    nice

  • @Manu-wb2uv
    @Manu-wb2uv 5 ปีที่แล้ว

    Does that means that every single child has 26 childrens ? Isn't that a lot of memory usage?

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

    thanks!

  • @haroonalishah1940
    @haroonalishah1940 6 ปีที่แล้ว +1

    Great, but your voice stuck and dies at the end of the statement. :/

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

      WTF are you doing here?

  • @mindfreak191191
    @mindfreak191191 7 ปีที่แล้ว

    A more intuitive sample of tries using dictionary - paste.ofcode.org/5jR9ApVSYV9qmaxx7cN36p

  • @SpaceFalconIO
    @SpaceFalconIO 8 ปีที่แล้ว

    Now try doing this in Javascript.

    • @endargon
      @endargon 8 ปีที่แล้ว +3

      Implying it would be more difficult ?

    • @TheHTMLCode
      @TheHTMLCode 8 ปีที่แล้ว +13

      why would doing it in JS be any more different? If you wanted to write it in assembly then I'd accept your point above :P

    • @ElcoBrouwervonGonzenbach
      @ElcoBrouwervonGonzenbach 6 ปีที่แล้ว +1

      class Node {
      constructor () {
      this.size = 0
      }
      add (s) {
      this.size++
      if (s.length === 0) {
      return
      }
      const current = s.shift()
      if (!this[current]) {
      this[current] = new Node()
      }
      this[current].add(s)
      }
      find (s) {
      if (s.length === 0) {
      return this.size
      }
      const child = this[s.shift()]
      if (!child) {
      return 0
      }
      return child.find(s)
      }
      }
      Gotta turn the input string into an array first: contact = op_temp[1].split('')

    • @garykim6649
      @garykim6649 6 ปีที่แล้ว

      Can you try to do it in machine language please?