Strings Making Anagrams: Looking for “Strings Making Anagrams” solution for Hackerrank problem? Get solution with source code and detailed explainer video.
A student is taking a cryptography class and has found anagrams to be very useful. Two strings are anagrams of each other if the first string’s letters can be rearranged to form the second string. In other words, both strings must contain the same exact letters in the same exact frequency. For example, bacdc and dcbac are anagrams, but bacdc and dcbad are not.
The student decides on an encryption scheme that involves two large strings. The encryption is dependent on the minimum number of character deletions required to make the two strings anagrams. Determine this number.
Given two strings, a and b, that may or may not be of the same length, determine the minimum number of character deletions required to make a and b anagrams. Any characters can be deleted from either of the strings.
Go to problem statement
Explanation Video:
Youtube Channel partner: CodingCart
Mentor: Satyendra Jaiswal
Langauge: Python
Source Code: for makeAnagram
function
# Complete the makeAnagram function below.
def makeAnagram(a, b):
c1=Counter(a)
for c in b:
c1[c]-=1
val=c1.values()
s=sum(abs(i) for i in val)
return s
Full Source Code:
#!/bin/python3
import math
import os
import random
import re
import sys
from collections import Counter
# Complete the makeAnagram function below.
def makeAnagram(a, b):
c1=Counter(a)
for c in b:
c1[c]-=1
val=c1.values()
s=sum(abs(i) for i in val)
return s
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
a = input()
b = input()
res = makeAnagram(a, b)
fptr.write(str(res) + '\n')
fptr.close()
Like this article? Follow us on Facebook and LinkedIn. You can also subscribe to our Youtube Channel.
This post is in partner with CodingCart.