Count letter differences of two strings

后端 未结 11 685
既然无缘
既然无缘 2020-12-09 17:11

This is the behaviour I want:

a: IGADKYFHARGNYDAA
c: KGADKYFHARGNYEAA
2 difference(s).
11条回答
  •  执笔经年
    2020-12-09 17:32

    a = "IGADKYFHARGNYDAA" 
    b = "KGADKYFHARGNYEAAXXX"
    match_pattern = zip(a, b)                                 #give list of tuples (of letters at each index)
    difference = sum (1 for e in zipped if e[0] != e[1])     #count tuples with non matching elements
    difference = difference + abs(len(a) - len(b))            #in case the two string are of different lenght, we add the lenght difference
    

提交回复
热议问题