【发布时间】:2021-09-03 16:54:33
【问题描述】:
我编写了 python 代码来检查需要从两个字符串中删除多少个字符才能使它们成为彼此的字谜。
这是问题陈述“给定两个字符串,and ,长度可能相同,也可能不同,确定生成和变位词所需的最小字符删除数。可以从任一字符串中删除任何字符”
def makeAnagram(a, b):
# Write your code here
ac=0 # tocount the no of occurences of chracter in a
bc=0 # tocount the no of occurences of chracter in b
p=False #used to store result of whether an element is in that string
c=0 #count of characters to be deleted to make these two strings anagrams
t=[] # list of previously checked chracters
for x in a:
if x in t == True:
continue
ac=a.count(x)
t.insert(0,x)
for y in b:
p = x in b
if p==True:
bc=b.count(x)
if bc!=ac:
d=ac-bc
c=c+abs(d)
elif p==False:
c=c+1
return(c)
【问题讨论】: