We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Nice solution, great idea! Here is the solution I found without using external libraries, and exploiting dictionaries instead:
def makeAnagram(a, b):
ma = {}
mb = {}
for i in range(len(a)):
if a[i] not in ma.keys():
ma.setdefault(a[i],1)
else:
ma[a[i]]+=1
for i in range(len(b)):
if b[i] not in mb.keys():
mb.setdefault(b[i],1)
else:
mb[b[i]]+=1
n = 0
for k in ma.keys():
if k not in mb.keys():
n += ma[k]
else:
if ma[k] != mb[k]:
n += abs(ma[k] - mb[k])
for k in mb.keys():
if k not in ma.keys():
n += mb[k]
return n
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Strings: Making Anagrams
You are viewing a single comment's thread. Return to all comments →
Nice solution, great idea! Here is the solution I found without using external libraries, and exploiting dictionaries instead:
def makeAnagram(a, b):