알고리즘/codefights
10>commonCharacterCount
Diademata
2017. 6. 28. 17:37
반응형
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". |
code>>
int commonCharacterCount(std::string s1, std::string s2) {
int count = 0;
int alphabets[26] = { 0, };
int alphabets1[26] = { 0, };
for (int i = 0; i < s1.size(); i++)
alphabets[s1[i] - 'a']++;
for (int i = 0; i < s2.size(); i++)
alphabets1[s2[i] - 'a']++;
for (int i = 0; i<26; i++)
{
if (alphabets[i] <= alphabets1[i])
count += alphabets[i];
else if (alphabets[i]>alphabets1[i])
count += alphabets1[i];
}
return count;
}
반응형