반응형
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;
}
반응형
'알고리즘 > codefights' 카테고리의 다른 글
12>sortByHeight (0) | 2017.06.28 |
---|---|
11>isLucky (0) | 2017.06.28 |
9>allLongestStrings (0) | 2017.06.28 |
8>matrixElementsSum (0) | 2017.06.28 |
7>almostIncreasingSequence (0) | 2017.06.28 |