Easy

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".

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string s1

    A string consisting of lowercase English letters.

    Guaranteed constraints:
    1 ≤ s1.length < 15.

  • [input] string s2

    A string consisting of lowercase English letters.

    Guaranteed constraints:
    1 ≤ s2.length < 15.

  • [output] integer

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

int commonCharacterCount(char *s1,char *s2)
{
	int alphabet1[26]={0, }, alphabet2[26]={0, }, count=0;

	for(int i=0;i<strlen(s1);i++)
		alphabet1[s1[i]-'a']++;
	for(int i=0;i<strlen(s2);i++)
		alphabet2[s2[i]-'a']++;

	for(int i=0;i<26;i++)
		count+=alphabet1[i]<alphabet2[i]?alphabet1[i]:alphabet2[i];

	return count;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Sort by Height  (0) 2020.04.05
<Codesignal> isLucky  (0) 2020.04.05
<Codesignal> All Longest Strings  (0) 2020.04.05
<Codesignal> matrixElementsSum  (0) 2020.04.05
<Codesignal> almostIncreasingSequence  (0) 2020.04.05

+ Recent posts