Friday, 23 October 2020

Strings: Making Anagrams by given two strings on Haccerrank Interview Preparation kit solution in python

 # Complete the makeAnagram function below.

def makeAnagram(a, b):
    lst1 = list(a)
    lst2 = list(b)
    print(lst1,lst2)
    i = -1
    c=0
    while(i<len(lst1)-1):
        i = i+1
        if lst1[i] in lst2:
            print(lst1[i])
        else:
            print("not in both")
            lst1.remove(lst1[i])
            c=c+1
            i=i-1
    print(lst1)
    i=-1
    while(i<len(lst2)-1):
        i = i+1
        if lst2[i] in lst1:

            print(lst2[i])
        else:
            print("not in both")
            lst2.remove(lst2[i])
            c=c+1
            i=i-1 
    print(lst2)  
    print("val of c",c)
    lst1.sort()
    lst2.sort()
    print(lst1,"\n",lst2)
    lst = list(set(lst1))
    for i in lst:
        if lst1.count(i) == lst2.count(i):
            print("same frew")
        else:
            diff = lst1.count(i) - lst2.count(i)
            print("dff in freq" , diff)
            if diff > 0:
                c= c+diff
            else:
                diff = -1*diff
                c= c+diff
                
    return c

No comments:

Post a Comment