Array | Anagram check | The Nerd Guy

Problem :

Given two strings, check to see if they are anagrams. An anagram is when the two strings can be written using the exact same letters (so you can just rearrange the letters to get a different phrase or word).

For Example :

''I love it when you call me senorita" is anagram of "senorita love me allc ouy it I hwen allc".
"my cat" and "matcy" are anagrams.
"call me" and "mallec" are also anagrams.


Solution :


Simple :


def anagram(s1,s2):
    s1=s1.lower()
    s2=s2.lower()
    sorted(s1)
    sorted(s2)
    if s1==s2:
        return True
    else:
        return False

Complex :

def anagram2(s1, s2):
    s1 = s1.replace(' ','').lower()
    s2 = s2.replace(' ','').lower()

    if not len(s1)==len(s2):
        return False
    
    count_var ={}
    
    for item in s1:
        if item in count_var:
            count_var[item] += 1
        else:
            count_var[item] = 1
            
    #print(count_var)
            
    for item in s2:
        if item in count_var:
            count_var[item] -= 1
        else:
            count_var[item] = 1
                    
    for item in count_var:
        if count_var[item] != 0:
            return False
        

    return True

#Keeppracticinglikeme

Comments

  1. Awesome information here I am so gleeful when i found your weblog while I was researching on Bing for something else,but believe me the way you interact is literally awesome I do respect that so much. I will instantly get your rss and stay informed of any updates you make and as well take the advantage to share some vital information regarding. how to legally live in cyprus I will also take the advantage to ask for your permission to join our TELEGRAM GROUP

    ReplyDelete

Post a Comment

Popular posts from this blog

Day 4: Class vs. Instance - HackerRank 30 days of code solution

Day 27: Testing - HackerRank 30 days of code solution

Day 11: 2D Arrays - HackerRank 30 days of code solution