How to compare two strings and check how many chars they have in common in Python

1 Answer

0 votes
from collections import Counter

def common_chars(s1, s2):
    return sum((Counter(s1) & Counter(s2)).values())


print(common_chars('pythonjava', 'pythonc++'))

print(common_chars('abcd', 'badc'))
 
 
 
 
'''
run:
 
6
4

'''

 



answered Jan 31, 2022 by avibootz
...