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

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

Knowledge Center

Knowledge Center

Күн бұрын

Пікірлер: 35
@leepakshiyadav1643
@leepakshiyadav1643 3 жыл бұрын
Thank you so much. Your way of explanation is amazing.
@KnowledgeCenter
@KnowledgeCenter 2 жыл бұрын
Glad it was helpful!
@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.
@chaengsaltz829
@chaengsaltz829 2 жыл бұрын
clever idea! love it. Thank you very much for making this vdo.
@KnowledgeCenter
@KnowledgeCenter 2 жыл бұрын
Most welcome 😊
@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.
@rishabharya7795
@rishabharya7795 4 жыл бұрын
Sir your videos are really good and they have helped me a lot
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
Glad to hear that
@mandeep2192
@mandeep2192 4 жыл бұрын
beautifully explained..
@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.
@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?
@0anant0
@0anant0 4 жыл бұрын
Thanks! Would you call this "suffix" trie instead of the regular "prefix" trie?
@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
@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.
@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.
@acxd
@acxd 4 жыл бұрын
Can anybody explain the meaning of the line Trie *t = this;
Random Point in Non-overlapping Rectangles | LeetCode 497 | C++, Python
26:35
Minimum Cost For Tickets | LeetCode 983 | C++, Java, Python
18:38
Knowledge Center
Рет қаралды 14 М.
Real Man relocate to Remote Controlled Car 👨🏻➡️🚙🕹️ #builderc
00:24
Twin Telepathy Challenge!
00:23
Stokes Twins
Рет қаралды 65 МЛН
PRANK😂 rate Mark’s kick 1-10 🤕
00:14
Diana Belitskay
Рет қаралды 12 МЛН
This Algorithm is 1,606,240% FASTER
13:31
ThePrimeagen
Рет қаралды 851 М.
LeetCode 1032. Stream of Characters
16:06
Happy Coding
Рет қаралды 522
I Solved 1583 Leetcode Questions  Here's What I Learned
20:37
ThePrimeTime
Рет қаралды 729 М.
American was shocked by 7 Slavic countries word differences!!
15:29
World Friends
Рет қаралды 471 М.
Introduction to Linked Lists (Data Structures & Algorithms #5)
18:47
House Robber | LeetCode 198 | C++, Java, Python
18:45
Knowledge Center
Рет қаралды 11 М.
Coding Interviews Be Like
5:31
Nicholas T.
Рет қаралды 6 МЛН
My Brain after 569 Leetcode Problems
7:50
NeetCode
Рет қаралды 2,7 МЛН
Leetcode - Stream of Characters (Python)
11:20
Timothy H Chang
Рет қаралды 333