I have many sets of 2 strings. I'm trying to determine the number of matching elements in these 2 strings. The rules are if the strings share a common letter, that's a point, order does matter, but each letter in the first string can only match one of the letters in the second string. So in the strings 'aaaab'
, 'acccc'
, only 1
point is awarded because there is only one 'a'
to match in the second string. Here are a few examples:
aaabb bbaaa 5
aabbb bbbaa 5
aaabb aabbb 4
aaabb ccaaa 3
aaaaa bbbbb 0
ababa babab 4
aabcc babaf 3
abcde abfgh 2
bacde abdgh 3
Hopefully that gets across how it works.
Here is the most efficient code I've been able to come up with, but its horribly convoluted. I hoping someone could think of something better.
def Score(guess, solution):
guess = list(guess)
solution = list(solution)
c = 0
for g in guess:
if g in solution and g != "_":
c += 1
solution[solution.index(g)] = "_"
return c
Surely this isn't the best way to do this, but I haven't been able to figure anything else out. I tried creating an algorithm with Counter
and doing guess&solution
, which worked, but ended up being way slower. Anyone have any ideas?