Easy
Given a string, find the number of different characters in it.
Example
For s = "cabca", the output should be
differentSymbolsNaive(s) = 3.
There are 3 different characters a, b and c.
Input/Output
-
[execution time limit] 0.5 seconds (c)
-
[input] string s
A string of lowercase English letters.
Guaranteed constraints:
3 ≤ s.length ≤ 1000. -
[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 differentSymbolsNaive(char *s)
{
int count=0;
bool *alphabet=(bool *)calloc(26,sizeof(bool));
for(int i=0;i<strlen(s);i++)
alphabet[s[i]-'a']=true;
for(int i=0;i<26;i++)
count+=alphabet[i];
return count;
}
728x90
'Codesignal' 카테고리의 다른 글
<Codesignal> growingPlant (0) | 2020.04.05 |
---|---|
<Codesignal> arrayMaxConsecutiveSum (0) | 2020.04.05 |
<Codesignal> firstDigit (0) | 2020.04.05 |
<Codesignal> extractEachKth (0) | 2020.04.05 |
<Codesignal> stringsRearrangement (0) | 2020.04.05 |