반응형

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.


code>>


int differentSymbolsNaive(std::string s) {

int alphabet[26] = { 0, };

int count = 0;

for (int i = 0; i<s.size(); i++) {

if (!alphabet[s[i] - 'a'])

{

alphabet[s[i] - 'a'] = 1;

count++;

}

}

return count;

}


다른 사람 코드 >>


int differentSymbolsNaive(std::string s) {

return std::set<char>(s.begin(), s.end()).size();

}

반응형

'알고리즘 > codefights' 카테고리의 다른 글

38>growingPlant  (0) 2017.08.07
37>arrayMaxConsecutiveSum  (0) 2017.08.07
35>firstDigit  (0) 2017.07.31
34>extractEachKth  (0) 2017.07.31
33>stringsRearrangement  (0) 2017.07.31

+ Recent posts