I'm currently doing one of the code fight's challenges, basically the challenge is to find the number of common characters between two strings. I came up with the solution below. I already passed the challenge, but I cannot figure out why I would have to iterate over the list twice and also not in every case. Thanks in advance for any explanations.
strings = [
["aabcc", "adcaa"],
["abca", "xyzbac"],
["assssbs", "aasaaaa"],
["zzzzzz", "zzz"],
["abcdefghxyzttw", "hgfedcbaabcwwt"] #<-- the strings failing with one iteration
]
def commonCharacterCount(s1, s2):
s1, s2 = sorted(list(s1)), sorted(list(s2))
matches = []
def matched(i):
matches.append(i)
s1.remove(i)
s2.remove(i)
[matched(i) for i in s1 if i in s2]
[matched(i) for i in s2 if i in s1]
[matched(i) for i in s1 if i in s2] #<-- Second for loop to find f
return len(matches)
def test():
for i in strings:
commonCharacterCount(i[0], i[1])
return
test()