Data Structures: Solve 'Contacts' Using Tries

  Рет қаралды 123,503

HackerRank

HackerRank

Күн бұрын

Пікірлер: 49
@chengqian5737
@chengqian5737 3 жыл бұрын
I watched lots of your video, you're the best who explains a problem clearly with easy understandable code.
@galfas09
@galfas09 7 жыл бұрын
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
@lg2389
@lg2389 8 жыл бұрын
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 жыл бұрын
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 жыл бұрын
size stands for the size of the node itself, not children's size.
@ultimatesin3544
@ultimatesin3544 7 жыл бұрын
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 жыл бұрын
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 жыл бұрын
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 жыл бұрын
Yes we have, but the description of this HackerRank task implies that there are no duplicate words in the input.
@ibrahimalshubaily9520
@ibrahimalshubaily9520 2 жыл бұрын
Thank you Gayle
@tomyang7788
@tomyang7788 7 жыл бұрын
what is the getCharIndex(char c) function for and why does it return c - a ?
@mnsayeed999
@mnsayeed999 7 жыл бұрын
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'
@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
@AbhishekSingh-op2tr
@AbhishekSingh-op2tr 7 жыл бұрын
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.
@gravity_vi
@gravity_vi 6 жыл бұрын
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'.
@haseebshahid3581
@haseebshahid3581 8 жыл бұрын
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 жыл бұрын
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.
@doktoren99
@doktoren99 7 жыл бұрын
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
@SulavTimsina
@SulavTimsina 7 жыл бұрын
How does the getCharIndex() method work?
@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?
@amlanch
@amlanch 4 жыл бұрын
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;
@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😂
@mangono8
@mangono8 8 жыл бұрын
why did she want the value at 5:35?
@ovu-t8d
@ovu-t8d 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
@MaminaZvezdochka
@MaminaZvezdochka 4 жыл бұрын
Thanks so much!
@emilianolaneri
@emilianolaneri 2 жыл бұрын
thanks!
@SuyashSoni248
@SuyashSoni248 7 жыл бұрын
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
@programming_and_snacks5192
@programming_and_snacks5192 6 жыл бұрын
I didn't understand code at all. Can someone explain it using example - "bat"
@victorriurean
@victorriurean Жыл бұрын
nice
@sukumarkuppusamy4218
@sukumarkuppusamy4218 7 жыл бұрын
findcount() is still confusing. Anybody please help with clear content.
@beastbeautybiker6656
@beastbeautybiker6656 4 жыл бұрын
She didn't update size before calling the findCount method recur.
@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
@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?
@Manu-wb2uv
@Manu-wb2uv 5 жыл бұрын
Does that means that every single child has 26 childrens ? Isn't that a lot of memory usage?
@haroonalishah1940
@haroonalishah1940 6 жыл бұрын
Great, but your voice stuck and dies at the end of the statement. :/
@eduanlenine
@eduanlenine 5 жыл бұрын
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 жыл бұрын
Implying it would be more difficult ?
@TheHTMLCode
@TheHTMLCode 8 жыл бұрын
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 7 жыл бұрын
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?
Data Structures: Solve 'Find the Running Median' Using Heaps
9:53
Tries - CS50 Shorts
16:24
CS50
Рет қаралды 76 М.
The evil clown plays a prank on the angel
00:39
超人夫妇
Рет қаралды 53 МЛН
Algorithms: Memoization and Dynamic Programming
11:17
HackerRank
Рет қаралды 977 М.
Data Structures: Tries
4:55
HackerRank
Рет қаралды 518 М.
AI Is Making You An Illiterate Programmer
27:22
ThePrimeTime
Рет қаралды 278 М.
Algorithms: Solve 'Ice Cream Parlor' Using Binary Search
9:29
HackerRank
Рет қаралды 103 М.
Data Structures: Anagram Problem Solution
6:41
HackerRank
Рет қаралды 107 М.
What Is A Trie and How Do We Build One In Python?
18:24
Nathan Patnam
Рет қаралды 16 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 692 М.
Data Structures: Trees
9:57
HackerRank
Рет қаралды 1 МЛН
Halloween is coming
0:12
Younes Zarou
Рет қаралды 3,4 МЛН
Лайфхак: Легально делать деньги
0:43
(✋❌)kageihina VS siajiwoo VS meosimmyyt VS oxzung#tiktok #shorts
0:12
изобрёл молоток мечты
0:55
Упоротый ПОВАР
Рет қаралды 543 М.
shocking end 🥴🤯 LeoNata family #shorts TikTok
0:54
LeoNata Family
Рет қаралды 41 МЛН
DID YOU NOTICE ANY LAPSES IN THE VIDEO or NOT? / MARGO_FLURY
0:34
MARGO FLURY | Маргарита Дьяченкова
Рет қаралды 12 МЛН