def jottoScore(s1,s2):
n = len(s1)
score = 0
sorteds1 = ''.join(sorted(s1))
sorteds2 = ''.join(sorted(s2))
if sorteds1 == sorteds2:
return n
if(sorteds1[0] == sorteds2[0]):
score = 1
if(sorteds2[1] == sorteds2[1]):
score = 2
if(sorteds2[2] == sorteds2[2]):
score = 3
if(sorteds2[3] == sorteds2[3]):
score = 4
if(sorteds2[4] == sorteds2[4]):
score = 5
return score
print jottoScore('cat', 'mattress')
我试图编写一个jottoScore函数,它将接受两个字符串并返回两个字符串之间共享的字符出现次数。在
即jottoScore('maat','caat')应该返回3,因为有两个As被共享,一个T被共享。在
我觉得这是一个足够简单的独立练习问题,但是我不知道如何迭代字符串并比较每个字符(我已经按字母顺序对字符串进行了排序)。在