Word Search ii | LeetCode 212 | C++, Java, Python

  Рет қаралды 7,928

Knowledge Center

Knowledge Center

Күн бұрын

Пікірлер: 24
@prabhatchanchal
@prabhatchanchal 4 жыл бұрын
Today I learnt trie completely....
@manpreetkhokhar5318
@manpreetkhokhar5318 4 жыл бұрын
Awesome as usual.😊
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
Thanks 😀
@siddharthsingh4330
@siddharthsingh4330 4 жыл бұрын
best explanation
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
Thanks 😀
@freshbots
@freshbots 4 жыл бұрын
Genius Sir
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
Thanks for the compliment.
@saianuhyakodimela4267
@saianuhyakodimela4267 4 жыл бұрын
Suppose if the board has words repeated, then the result list will have duplicate word entries. I would like to know where you are stopping the search once the word is found.
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
We are storing it in a set, and set doesn't allow duplicate entries. While returning we are converting it to vector or list as per requirement of the question.
@shashankagrawal2378
@shashankagrawal2378 4 жыл бұрын
Java : in line 30 , as soon as the word is inserted into the result Set, the method should return, right?
@AakashMishra21
@AakashMishra21 4 жыл бұрын
Great video! I wonder if you could dfs through the entire board without using that double loop. I spent a lot of time tying that but finally had to give up. What do you think?
@surajkiranreddymothe686
@surajkiranreddymothe686 4 жыл бұрын
Hello, Suppose "ao" was also a word present in words vector, does this code add "ao" to the result vector? I see you are changing the characters in board to dollar sign and also passing the board by reference. When dfs starts at 'a' [coordinates --- (0,1)] and visits 'o' [coordinates - (0,0)], 'o' will be in a dollar sign and ao will not be added to the result right?
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
"ao" will be added to the result. When DFS starts from (0,0) i.e., 'o', once this dfs call starting from (0,0) terminates, the old character value is restored, i.e., '$' is replcaed back to 'o'. And "ao" is not starting with 'a'. So, here it will not be added. When a fresh dfs() is triggered with empty string "" from (0,1) i.e., 'a', then only it will search for all the words starting with 'a', then "ao" will be searched, and added to result.
@surajkiranreddymothe686
@surajkiranreddymothe686 4 жыл бұрын
Ohh. My understanding was whenever we use & sign for a variable while passing it, that variable will be changed permanently whenever there is a change to it at any part of recursion. For example, result variable in your code, it will be changed permanently whenever we push back a word to it. Note: "permanently" here means even when the recursion rolls back, the change that happened to that variable remains intact. It looks like, I need to strengthen my passing by reference concept. Do you suggest any source to learn from?
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
That change is permanent only. But, dfs call starting from (0,0) terminates after finding "oauth". And all the changed characters are restored back to original. Only after that fresh dfs call is made from "a" or (0,1). When dfs() was called for (0,1) earlier as a neighbour of 'o', then we were not passing empty string. So, further dfs triggerred from a would pass "oa" as string and so on. So, "ao" would not be searchable any way, since we will be appending to "oa". When all this completes, old state is restored, Then we trigger a fresh dfs() from (0,1) or 'a', passing empty string. So, first 'a' will be appended. Then "a" will passed to dfs(0,0, "a"). Then at (0,0), it will see that if I append current character, i.e, 'o', string becomes "ao", which is present in Trie. Then it would be added to result. At any point of time there is just single instance of board vector.
@surajkiranreddymothe686
@surajkiranreddymothe686 4 жыл бұрын
Gotcha. Thanks a lot. I appreciate your patience.
@mukultaneja7243
@mukultaneja7243 4 жыл бұрын
Thanks sir for the great explanation ! Please tell why we didn't declare the object on the heap ( as Trie* trie = new Trie() ) , but on stack ?? coz we don't know how many words we are gonna insert in the trie.
@shubhangigupta2211
@shubhangigupta2211 4 жыл бұрын
Hey, it's mentioned in the comment above
@mukultaneja7243
@mukultaneja7243 4 жыл бұрын
@@shubhangigupta2211 Oh I didn't see that. Thankyou !
@akakop
@akakop 4 жыл бұрын
thanks
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
Welcome
@surajkiranreddymothe686
@surajkiranreddymothe686 4 жыл бұрын
1. Trie*trie=new Trie(); for(string w:words){ trie->insert(word); } 2. Trie trie; for(string w:words){ trie.insert(w); } Which one do you suggest to use?
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
We don't return reference of a stack variable to other function, but here we are using trie much before returning from findWords() function, so it's safe to use case 2. Stack will be cleared only after findWords() ends. In case 1 we are allocating on Heap. So, it's our job to delete trie; before returning the result. Otherwise if this call is made multiple times, every time memory will be allocated on heap but nor cleared up, so memory leak. Also Stack memory is contiguous and may be faster, but the main concern can be manual allocation and de-allocation in case of Heap. We should use Heap, when we need more flexibility, like resizing allocated space, or size can not be determined at compiled time, or very large chunk of memory is required.
@surajkiranreddymothe686
@surajkiranreddymothe686 4 жыл бұрын
Makes Sense. Thank you very much.
Word Search II - Backtracking Trie - Leetcode 212 - Python
20:54
The Best Band 😅 #toshleh #viralshort
00:11
Toshleh
Рет қаралды 22 МЛН
黑天使只对C罗有感觉#short #angel #clown
00:39
Super Beauty team
Рет қаралды 36 МЛН
Tuna 🍣 ​⁠@patrickzeinali ​⁠@ChefRush
00:48
albert_cancook
Рет қаралды 140 МЛН
Word Search | LeetCode 79 | C++, Java, Python3
28:04
Knowledge Center
Рет қаралды 30 М.
How C++ took a turn for the worse
5:03
Code Persist
Рет қаралды 329 М.
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 637 М.
Word Search II | DFS + Map | DFS + TRIE | Leetcode #212
24:49
Top 7 Algorithms for Coding Interviews Explained SIMPLY
21:22
Codebagel
Рет қаралды 456 М.
Top 7 Data Structures for Interviews Explained SIMPLY
13:02
Codebagel
Рет қаралды 239 М.
Hash Tables and Hash Functions
13:56
Computer Science Lessons
Рет қаралды 1,6 МЛН
How I Mastered Data Structures and Algorithms in 8 Weeks
15:46
Aman Manazir
Рет қаралды 129 М.
The Best Band 😅 #toshleh #viralshort
00:11
Toshleh
Рет қаралды 22 МЛН