ManipulationStrings: Making Anagrams

Alice is taking a cryptography class and finding anagrams to be very useful. We consider two strings to be 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.

Alice decides on an encryption scheme involving two large strings where encryption is dependent on the minimum number of character deletions required to make the two strings anagrams. Can you help her find this number?

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the makeAnagram function below.
def makeAnagram(a, b):
    x1=[0]*26  ####
    x2=[0]*26
    for c in a:
        j=ord(c)-ord('a') ####
        x1[j]+=1
    for c in b:
        j=ord(c)-ord('a')
        x2[j]+=1
    result=sum(abs(x1[i]-x2[i]) for i in range(26))
    return result    

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()

.

发布了163 篇原创文章 · 获赞 90 · 访问量 6291

猜你喜欢

转载自blog.csdn.net/weixin_45405128/article/details/104204505
今日推荐