Check if two strings are anagrams

แชร์
ฝัง
  • เผยแพร่เมื่อ 9 พ.ย. 2024

ความคิดเห็น • 33

  • @muhammadzakiahmad8069
    @muhammadzakiahmad8069 4 หลายเดือนก่อน

    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

  • @Anikashukla1808
    @Anikashukla1808 2 หลายเดือนก่อน

    whats the time complexity and space complexity ?

  • @pitrya2533
    @pitrya2533 ปีที่แล้ว +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 2 หลายเดือนก่อน

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

    • @arpitagupta4997
      @arpitagupta4997 2 หลายเดือนก่อน

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

  • @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.

  • @pavankolli4490
    @pavankolli4490 2 ปีที่แล้ว +4

    Boss is Back 😎

    • @CppNuts
      @CppNuts  2 ปีที่แล้ว

      Thanks hahaha

  • @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.

  • @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!

  • @notpipa_
    @notpipa_ 2 ปีที่แล้ว

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

  • @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.

  • @ChandraShekhar-by3cd
    @ChandraShekhar-by3cd 2 ปีที่แล้ว

    Thanks a lot for your informative video. 😃

    • @CppNuts
      @CppNuts  2 ปีที่แล้ว

      Thanks man!!

  • @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..

  • @duonghouc7184
    @duonghouc7184 2 ปีที่แล้ว

    wonderful video

  • @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

  • @badassopenpolling
    @badassopenpolling 2 ปีที่แล้ว

    An important video.

    • @CppNuts
      @CppNuts  2 ปีที่แล้ว

      Thanks

  • @cheelavaishnavi9125
    @cheelavaishnavi9125 8 หลายเดือนก่อน

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