Stream of Characters | LeetCode 1032 | C++, Java, Python

  Рет қаралды 4,958

Knowledge Center

Knowledge Center

Күн бұрын

Пікірлер: 35
@100bands
@100bands 4 жыл бұрын
Nice explanation. Instead using stringbuilder to reverse the string, I looped from end of string when inserting. The runtime reduced to 82ms and beats 96%
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
Great.
@crankyinmv
@crankyinmv 4 жыл бұрын
Thanks for the video. Instead of matching a word from a stream, I made my Trie stateful so I could proceed 1 character at a time. Other than that, same approach.
@0anant0
@0anant0 4 жыл бұрын
I think such a stateful trie would be helpful in LC 676 (magic dict) where we change one char and then check if the remaining trie contains matches remaining chars. (so the state would tell you the location in trie where all prev chars have matched). Please let me know if this is correct.
@crankyinmv
@crankyinmv 4 жыл бұрын
@@0anant0 Yes, that is how I was keeping track of state. Really helped me out in Word Search II.
@leepakshiyadav1643
@leepakshiyadav1643 3 жыл бұрын
Thank you so much. Your way of explanation is amazing.
@KnowledgeCenter
@KnowledgeCenter 3 жыл бұрын
Glad it was helpful!
@midasama3124
@midasama3124 4 жыл бұрын
Nice implementation! I wouldn't have come up with reversing the strings before inserting them into the trie. Thanks!
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
The idea should strike you, although it may not during interviews due to pressure. We are always concerned with suffixes of the stream, i.e., curr-k to curr. We have to match stream[curr - k], .... stream[curr-1], stream[curr]. But, we don't know what k. So, we start from curr, curr-1, curr-2, ...... i.e., in reverse order. And we were almost almost always going to be using Trie(Prefix Tree) for this kind of plain search, where a list of well-defined words is given beforehand. So, we have to store the words in reverse order.
@midasama3124
@midasama3124 4 жыл бұрын
@@KnowledgeCenter Yeah, I get how this is useful. It is just that it didn't come up to me that easily and it would probably take me a while to figure it out on my own.
@pratyushranjansahu9495
@pratyushranjansahu9495 4 жыл бұрын
I always wait for you video to upload everyday. I forget to like ur video some days , but i really like ur video. I observed that regularly one person is dislike ur video. I don't know whether he is going through your video or not... :).
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
Thanks. It doesn't matter. A few dislikes is expected. As long as majority of people are liking the videos, it keeps me motivated.
@chaengsaltz829
@chaengsaltz829 2 жыл бұрын
clever idea! love it. Thank you very much for making this vdo.
@KnowledgeCenter
@KnowledgeCenter 2 жыл бұрын
Most welcome 😊
@rishabharya7795
@rishabharya7795 4 жыл бұрын
Sir your videos are really good and they have helped me a lot
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
Glad to hear that
@ashishaggarwal1842
@ashishaggarwal1842 4 жыл бұрын
Nice one!
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
Thanks!
@neeteshkumarchaurasia7509
@neeteshkumarchaurasia7509 3 жыл бұрын
What is the format used for declaring the trie constructor? I am unable to find this format "ClassName(): variable(value) {}". Thanks
@manupatet
@manupatet 4 жыл бұрын
Let me first thank you for taking time to diligently upload videos everyday. There is dense content in your videos and I'm sure it is helping a number of people. My question is, how did you come up with the reversing of the strings technique? Is it a known way of doing certain class of problems ? As Miguel observes, it seems difficult, if not impossible, to come up with such an insight during an interview.
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
The idea should strike you, although it may not during interviews due to pressure. We are always concerned with suffixes of the stream, i.e., curr-k to curr. We have to match stream[curr - k], .... stream[curr-1], stream[curr]. But, we don't know what k. So, we start from curr, curr-1, curr-2, ...... i.e., in reverse order. And we were almost almost always going to be using Trie(Prefix Tree) for this kind of plain search, where a list of well-defined words is given beforehand. So, we have to store the words in reverse order.
@0anant0
@0anant0 4 жыл бұрын
Thanks! Would you call this "suffix" trie instead of the regular "prefix" trie?
@mandeep2192
@mandeep2192 4 жыл бұрын
beautifully explained..
@srikantsharma6430
@srikantsharma6430 4 жыл бұрын
Nice approach!
@srikantsharma6430
@srikantsharma6430 4 жыл бұрын
Can you please tell me the Space complexity of this solution? Is it O(c), where c = content size = total space to store all words in Trie?
@prashantdutt514
@prashantdutt514 4 жыл бұрын
can you please share the documentation of how to use "this" directly i only saw the use of "this" in "this->val = ... " manner
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
'this' is simply a pointer to the object of the given class. So, from some code we call: Trie t; t.foo(); Then inside the foo(), 'this' is equivalent to Trie *tp = &t; As if you are passing the pointer 'tp' to the foo() function.
@prashantdutt514
@prashantdutt514 4 жыл бұрын
Thank you sir
@ShivamPandey-wd9ro
@ShivamPandey-wd9ro 4 жыл бұрын
Sir Why don't we add every character of input into a set. So every time the check function is called for a character, the contains method of set can be used?
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
Can you elaborate more. Order of characters is important. Just character is not enough. Lets say a suffix of stream is ...abccd. So, latest query is query('d'). And let's say there is a word "abcd" in dictionary. If you store characters of stream in set, you will find all characters of abccd and return true, but it should have been false, since there is no word "abccd" in dictionary. it's "abcd".
@ShivamPandey-wd9ro
@ShivamPandey-wd9ro 4 жыл бұрын
@@KnowledgeCenter Ok sir, thank you for explaination. I understand now.
@acxd
@acxd 4 жыл бұрын
Can anybody explain the meaning of the line Trie *t = this;
@SAHILVAID30
@SAHILVAID30 4 жыл бұрын
sir I have two doubts: 1. Why didn't we pass stream as a stack instead of deque 2. Why do we have to pass by refrence
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
We pass by reference so that we don't copy the complete stream again. In stack, only latest character pushed to it can be accessed, to get previous characters, we need to pop() elements. We are only inserting into the stack. If you pass a copy you could have popped elements from the stack. But, copying would be costly. Deque is not the only way, we can use vectors, list etc.
Minimum Cost For Tickets | LeetCode 983 | C++, Java, Python
18:38
Knowledge Center
Рет қаралды 14 М.
Random Point in Non-overlapping Rectangles | LeetCode 497 | C++, Python
26:35
Quando A Diferença De Altura É Muito Grande 😲😂
00:12
Mari Maria
Рет қаралды 44 МЛН
99.9% IMPOSSIBLE
00:24
STORROR
Рет қаралды 31 МЛН
Une nouvelle voiture pour Noël 🥹
00:28
Nicocapone
Рет қаралды 6 МЛН
Правильный подход к детям
00:18
Beatrise
Рет қаралды 10 МЛН
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 172 М.
LeetCode 1032. Stream of Characters
16:06
Happy Coding
Рет қаралды 523
Gitlab DELETING Production Databases | Prime Reacts
17:27
ThePrimeTime
Рет қаралды 357 М.
House Robber | LeetCode 198 | C++, Java, Python
18:45
Knowledge Center
Рет қаралды 11 М.
C++ vs Rust: which is faster?
21:15
fasterthanlime
Рет қаралды 407 М.
Edit Distance | Dynamic Programming | LeetCode 72 | C++, Java, Python
27:22
Coding Interviews Be Like
5:31
Nicholas T.
Рет қаралды 6 МЛН
Quando A Diferença De Altura É Muito Grande 😲😂
00:12
Mari Maria
Рет қаралды 44 МЛН