Check if two strings are anagrams

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

КОМЕНТАРІ • 34

  • @pavankolli4490
    @pavankolli4490 2 роки тому +4

    Boss is Back 😎

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

    whats the time complexity and space complexity ?

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

    Can i use two for loops to count each word characters count and then return true if map1 == map2?

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

    Hey buddy whr were u??
    Please keep uploading videos...i really love your content...

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

      Thanks man, sure I’ll do.

  • @ChandraShekhar-by3cd
    @ChandraShekhar-by3cd 2 роки тому

    I guess we can avoid one additional loop, we can increment the count while iterating over first string and decrement the count of map for the second element. At the end we can iterate over the map if any all character has 0 count then it is anagram else it is not. Please correct me if I am wrong.

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

      How r u populating Map which you are decrementing?

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

      @@CppNuts we can delete key from map if after decrement Map[b[I]] becomes zero
      last loop can be replaced with: return Map.empty();
      this method will be faster because decrement loop can break if there is no more that symbol in Map

    • @ChandraShekhar-by3cd
      @ChandraShekhar-by3cd 2 роки тому

      @@CppNuts Since both string will be having same length , in that same for lopp we can increment the count of character from string 1 and decrement the count of the char for the other string.

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

      Got it, yes that is correct, but in last solution we don't need 3rd loop that's what i asked in the end of the video.
      So in the end we have 2 loops in both the solutions.
      Good for you to think this much for given problem, then only you guys can get the max benefits.

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

    How about having two sum variables and adding of ascii values for two string characters, if string length is same, in single for loop and comparing both sum are equal for an anagram

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

      Hi Shriram, No this wont work.

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

      @@CppNuts Yeah true👍 my bad one such example is "ac" & "bb"...thanks for reply!

  • @muhammadzakiahmad8069
    @muhammadzakiahmad8069 6 місяців тому

    My python implementation:
    def is_Anagram(texta, textb):
    if len(texta) != len(textb):
    return False
    for char in texta:
    if texta.count(char) != textb.count(char):
    return False
    return True

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

    I want to know if that last loop is required?

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

      Just think…

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

      @@CppNuts I think it is not required. It can be avoided as the conditions above last loop cover all possible scenarios

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

      Correct..

    • @HitanshSharma-q2l
      @HitanshSharma-q2l 23 дні тому

      Yes it is required reason:
      if the s1 contains some big string--> "abcd" and the s2 contains string like "abc" then even if the s1 left out with extra char "d", your code will return "true" for this... hence, if we have to check the i.second should be ==0

  • @pitrya2533
    @pitrya2533 2 роки тому +3

    or you can just add the ascii numbers and compare. say numbers a-z as 1-26 then string "car" will add up to 3+1+18 = 22 and "arc" will be the same.

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

      what will be the time complexity and space complexity for this?

    • @arpitagupta4997
      @arpitagupta4997 4 місяці тому +1

      No , 10+2 = 8+4 = 12 for eg. This will lead to incorrect results

  • @ChandraShekhar-by3cd
    @ChandraShekhar-by3cd 2 роки тому

    Thanks a lot for your informative video. 😃

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

    wonderful video

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

    Thank you for the great content. It is helping to prepare for interviews.
    How can we make this solution, case insensitive. Currently it is case sensitive. If "Listen", "silent" - it's saying, not anagram. If "listen", "silent" - it's saying, anagram. Please suggest. We will have to check it's ascii value and convert ascii value to small case lettter asciis, I think and store that in the same map. Or at the beginning itself, convert both strings to lower case or upper case.
    Added below lines and it's working fine.
    #include //for transform function
    transform(one.begin(), one.end(), one.begin(), ::tolower);
    transform(two.begin(), two.end(), two.begin(), ::tolower);
    //where one and two are string variables

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

      Converting in the beginning will be very easy, if interviewer is ok with that then grt, otherwise check what he/she wants in this case.

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

    After long time

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

      Yes next is vptr and vtable.
      Or i will complete this series first then will go for other topics.

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

      @@CppNuts please make video for dll and move constructor

  • @cheelavaishnavi9125
    @cheelavaishnavi9125 11 місяців тому

    how to do for more than 2 words like
    if cat tac act dog god
    we have to get act tac cat
    dog god

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

    An important video.