Group Anagrams (LeetCode 49) | Full solution with 2 methods and examples | Study Algorithms

Поділитися
Вставка
  • Опубліковано 2 лют 2025

КОМЕНТАРІ • 65

  • @dhavalchaudhary1786
    @dhavalchaudhary1786 10 місяців тому +10

    I generally not hit like on youtube videos, but this guy nailed it. Thanks for your community contribution NIkhil.

  • @devprakash5320
    @devprakash5320 3 роки тому +17

    loved your explanation . But 1 thing I would like to correct . The time complexity of method 1 will be O(n k log k ) , where k is the length of the string , n is total strings . This is because sorting a string would take (k log k ) . Any way awesome explanation . thanks

  • @WhosShamouz
    @WhosShamouz Рік тому +5

    I was missing ONE idea and it popped in my head when I saw your first method, amazing explanation!

  • @amitbhattacharya356
    @amitbhattacharya356 2 роки тому +11

    I got really surprised by so a vivid and clear explanation. I appreciate your efforts.

    • @nikoo28
      @nikoo28  2 роки тому

      Glad I could help you out 😄

  • @vikashsharma9678
    @vikashsharma9678 5 місяців тому +1

    sir you explaination to any algorithm is the best explanation i had ever seen ...only your explanation is enough to solve the problem no need to see the code

  • @akashddeepchitransh4537
    @akashddeepchitransh4537 2 місяці тому

    Hey, you are super underrated, I didn't know about you channel but now I know and will be utilizing it in my preparation.

  • @plutomessi21
    @plutomessi21 Рік тому +4

    Dhanyavad bhaiya, you are the best teacher I have seen on youtube😭

  • @serendipity1328
    @serendipity1328 2 місяці тому +4

    You can avoid unnecessary d0eof0 in 2nd solution by putting condition ...if (i != 0) then only append!

  • @ricardohernandezmendez4207
    @ricardohernandezmendez4207 8 місяців тому

    At the beginning it was difficult to find out how to solve this problem, but with this explanation it make it easy to solve it. Thank.

  • @ShaziyaBanu-d9
    @ShaziyaBanu-d9 Місяць тому +1

    Excellent explaination..!! Very clean and clear. I Loved It.💝💝

  • @Vishal-8995
    @Vishal-8995 Рік тому +3

    Premium Explanation

  • @yusufnurwahid898
    @yusufnurwahid898 8 місяців тому +1

    Very clear explanation! Keep up!!! 👍🏻

  • @akshanshsharma8157
    @akshanshsharma8157 11 місяців тому +3

    In the sorting way, the time complexity is not O(nlogk) but rather O(n.klogk)

  • @mazthespaz1
    @mazthespaz1 7 місяців тому +1

    i'm new to python and this was my solution
    def smash(str):
    tmp = 1
    orda = ord('A')-1
    ls = len(str)

    for i in range( ls ):
    tmp = tmp * (ord(str[i]) - orda)
    return tmp
    def groop(wordlist):
    grope = {}

    for word in wordlist:
    hashy = smash(word)
    if hashy not in grope:
    grope[hashy] = []
    grope[hashy].append(word)

    return [val for val in grope.values()]
    if __name__ == '__main__':
    # arr = [ 'cat','tea','tan','ate','nat','bat' ]
    arr = [ 'eat','cars','tea','scar','a','listen','silent']
    print (groop(arr))

    • @mazthespaz1
      @mazthespaz1 7 місяців тому

      this solution worked in codewars but not leetcode. i need to refine the smash function to prevent duplicate hashes

  • @ElinaAdibi-b1t
    @ElinaAdibi-b1t 4 місяці тому

    Thank you, you explained it very well!

  • @shaileshsathe9779
    @shaileshsathe9779 2 роки тому

    Very well explained the techniques, code and dry-run. Great work Nikhil.

    • @shaileshsathe9779
      @shaileshsathe9779 2 роки тому

      I don't know why, I ran both techniques sorting and frequency but sorting is showing less time than frequency method. sorting => 15ms frequency = 29ms. I believe its random.

    • @aryamiraozdemir
      @aryamiraozdemir Рік тому

      @@shaileshsathe9779 I'm not sure but I think it might be because frequency string has to be sorted so it takes even longer to count everything and sort the letters alphabetically

  • @Donkle365
    @Donkle365 2 роки тому +1

    Hey, I found this video trough the article and thank you for all the work, it really helped me. The solution of method 2 at line 34 has a mistake: "freq++;", you can't ++ an array, so we need to do freq[c - 'a']++ :)

  • @edd_gos
    @edd_gos 5 місяців тому

    bro, thank you for the video. it was very usefull (like all your videos)

  • @hersheynohara5887
    @hersheynohara5887 Рік тому

    That's an amazing explanation! 👏👏

  • @ramsidh2218
    @ramsidh2218 2 роки тому

    great explanation with animation step by step

  • @RohitSharma-q8j1p
    @RohitSharma-q8j1p 9 місяців тому

    great video and best ever explaination ever

  • @zanefalcao3230
    @zanefalcao3230 5 місяців тому

    very nice explanation

  • @cuteangel1726
    @cuteangel1726 2 роки тому

    Appreciate your effort !!!

  • @jst8922
    @jst8922 Рік тому

    8:14 should be "nat", not sorted string "ant".
    Otherwise your tutorials are the best regarding leetcode problems, because you explain with examples what should the code do.

  • @shahbazhussain2128
    @shahbazhussain2128 4 місяці тому

    If I use sorting logic will be my solution wrong?
    I am facing difficulty in understanding the HasMap and Frequency Concept.

  • @Babas420
    @Babas420 Рік тому +1

    Great video as usual thanks for this! PS its not pronounced vo-i-la but just vo-la, hope this helps!

  • @MahiM-ze4hm
    @MahiM-ze4hm 5 місяців тому

    Awesome 👏✊👍

  • @hazerasarker2442
    @hazerasarker2442 Рік тому

    Thank you Nikhil :D

  • @cricworld5806
    @cricworld5806 2 роки тому

    sir awesome explanation ...................

  • @abhishekshankar6305
    @abhishekshankar6305 Рік тому

    you are the best

  • @anoohyaveerapaneni8703
    @anoohyaveerapaneni8703 6 днів тому

    which is has more time complexity ?

  • @yfchina143
    @yfchina143 10 місяців тому

    this there any better way for a frequency string algothrim?

  • @lofiboy7866
    @lofiboy7866 3 місяці тому

    We need to write getfrequency string string method also sep?

  • @mujeebbridgetown
    @mujeebbridgetown 2 роки тому

    Nice explanation

  • @abinashpanda393
    @abinashpanda393 Рік тому

    This might not pass all test cases, you have to add a delimiter like '#' while concatenating numbers in freq variable.

    • @nikoo28
      @nikoo28  Рік тому

      passes all cases on Leetcode as per the problem constraints. If you have different strings patterns, it might need a little tweaking.

    • @abinashpanda393
      @abinashpanda393 Рік тому

      ​@@nikoo28 You are right for ["bdddddddddd","bbbbbbbbbbc"] this will not going to pass. Probably new test cases have been added.
      Thank you for your response. Your tutorials are really helpful.

  • @sharathprathapagiri8416
    @sharathprathapagiri8416 Місяць тому

    you should have initialized char as something like char_count, initializing it with "c" makes the program confusing.

    • @nikoo28
      @nikoo28  Місяць тому

      char 'c' is a very standard practice in competitive programming.

  • @GHT0310
    @GHT0310 Місяць тому

    sirr !

  • @sayanmanna2511
    @sayanmanna2511 Рік тому +1

    But why light theme?? I am blind now..

    • @nikoo28
      @nikoo28  Рік тому

      Light is easier for diagrams and animations. Will keep using light theme.

  • @yogeshganpule2695
    @yogeshganpule2695 7 місяців тому

    tried watching but you just explained what the code does and its too confusing for me to undertsand what
    for each
    freq [c -'a']++;
    Does

    • @nikoo28
      @nikoo28  7 місяців тому +3

      c is the actual character…let us say it is character ‘k’
      And we do c - ‘a’
      What happens internally is both are converted to ascii
      Small ‘a’ has ascii 97
      The character ‘k’ has ascii 107
      So when you do c - ‘a’ we get 107-97 = 10
      That means freq[10]
      This represents 11th character as array is 0 based indexing
      Means we are referring to frequency of 11th character in the English alphabet which is letter ‘k’

    • @yogeshganpule2695
      @yogeshganpule2695 7 місяців тому

      @@nikoo28 Thanks .

  • @devansh_4u
    @devansh_4u Рік тому +1

    beats 30% java codes, wonder what could be a better solution!

    • @nikoo28
      @nikoo28  Рік тому +1

      If you are concerned about having an overall faster solution, C++ should be the preferred language.
      Even with Java you can leverage some sort of caching and pre-processing of some results to obtain faster solutions. Generally not needed.

    • @devansh_4u
      @devansh_4u Рік тому

      @@nikoo28 thanks sir

  • @fourieruddin871
    @fourieruddin871 Рік тому +1

    Best

  • @studies3943
    @studies3943 6 місяців тому +1

    your accent is sweet

  • @himadripaul7332
    @himadripaul7332 2 роки тому

    Excellent ... But disheartened to see less subscribers ... ☹️😑🙂

    • @nikoo28
      @nikoo28  2 роки тому +2

      Yea…I don’t understand why my channel does not show up in results and suggestions. I have been doing everything possible: video descriptions, links etc.

    • @shaileshsathe9779
      @shaileshsathe9779 2 роки тому

      @@nikoo28 You can add keywords like Leetcode, Anagram, Interview Prep just like you added for String.Maybe it will work

  • @boomshakalaka8567
    @boomshakalaka8567 2 роки тому

    There's nothing correct about this video. Lol. Indian programmers lulz

    • @nikoo28
      @nikoo28  2 роки тому +2

      did you find some problem in the explanation or the method? Always up for improvement...

    • @Sarvy001
      @Sarvy001 2 роки тому

      @@nikoo28
      I have found another solution here.
      Not explained in detail as you have done, but this one has less lines of code. ua-cam.com/video/fAJqAnEWq0o/v-deo.html&ab_channel=ThatIndianCoder

    • @Nishi-Tiwari
      @Nishi-Tiwari Рік тому +1

      @@nikoo28 You are best!