본문 바로가기

Programming/Python

[python] codeSignal 문제풀이 (10)

10. Given two strings, find the number of common characters between them.

 

Example)

For s1 = "aabcc" and s2 = "adcaa", the output should be commonCharacterCount(s1, s2) = 3.

Strings have 3 common characters - 2 "a"s and 1 "c".

1
2
3
4
5
6
def commonCharacterCount(s1, s2):
    count = 0
    common_ch = set(s1) & set(s2)
    for i in common_ch:
        count += min(s1.count(i), s2.count(i))
    return count
1
2
3
4
5
# solution code
def commonCharacterCount(s1, s2):
    com = [min(s1.count(i),s2.count(i)) for i in set(s1)]
    return sum(com)