Common Character Count
Given two strings, find the number of common characters between them.
Example
For s1 = "aabcc"
and s2 = "adcaa"
, the output should be
common_character_count(s1, s2) = 3
Strings have 3
common characters - 2
"a"s and 1
"c".
Solution
py
def common_character_count(s1, s2):
count = 0
for i in s1:
for j in range(len(s2)):
if i == s2[j]:
count += 1
s2 = s2[:j] + s2[j+1:]
break
return count
print(common_character_count("aabcc", "adcaa"))
js
function commonCharacterCount(s1, s2) {
let count = 0;
for (let i = 0; i < s1.length; i++) {
if (s2.includes(s1[i])) {
count++;
s2 = s2.replace(s1[i], '');
}
}
return count;
}
console.log(commonCharacterCount('aabcc', 'adcaa'));