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

Sdílet
Vložit
  • čas přidán 6. 09. 2024

Komentáře • 24

  • @prabhatchanchal
    @prabhatchanchal Před 4 lety +3

    Today I learnt trie completely....

  • @surajkiranreddymothe686
    @surajkiranreddymothe686 Před 4 lety +1

    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  Před 4 lety +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 Před 4 lety +1

      Makes Sense. Thank you very much.

  • @saianuhyakodimela4267
    @saianuhyakodimela4267 Před 4 lety +1

    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  Před 4 lety

      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 Před 4 lety

    Java : in line 30 , as soon as the word is inserted into the result Set, the method should return, right?

  • @surajkiranreddymothe686
    @surajkiranreddymothe686 Před 4 lety +1

    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  Před 4 lety

      "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 Před 4 lety

      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  Před 4 lety +1

      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 Před 4 lety

      Gotcha. Thanks a lot. I appreciate your patience.

  • @AakashMishra21
    @AakashMishra21 Před 4 lety

    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?

  • @freshbots
    @freshbots Před 4 lety +1

    Genius Sir

  • @akakop
    @akakop Před 4 lety +2

    thanks

  • @siddharthsingh4330
    @siddharthsingh4330 Před 4 lety +1

    best explanation

  • @mukultaneja7243
    @mukultaneja7243 Před 4 lety

    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.

  • @manpreetkhokhar5318
    @manpreetkhokhar5318 Před 4 lety +1

    Awesome as usual.😊