今天讲一道 codewars 6级题。
纯手工视频,无修正,希望下期能讲难一点的题。不过算法题讲起来都是数学问题,想抛掉数学写代码还是难啊。
腾讯讲解视频链接
https://v.qq.com/x/page/v0715ap9lfa.html
B站讲解视频链接
https://www.bilibili.com/video/av26409430
题目
Count the number of Duplicates
Write a function that will return the count of distinct case-insensitive alphabetic characters and numeric digits that occur more than once in the input string. The input string can be assumed to contain only alphabets (both uppercase and lowercase) and numeric digits.
Example
"abcde" -> 0 # no characters repeats more than once
"aabbcde" -> 2 # 'a' and 'b'
"aabBcde" -> 2 # 'a' occurs twice and 'b' twice (bandB)
"indivisibility" -> 1 # 'i' occurs six times
"Indivisibilities" -> 2 # 'i' occurs seven times and 's' occurs twice
"aA11" -> 2 # 'a' and '1'
"ABBA" -> 2 # 'A' and 'B' each occur twice
SOLUTION
size_t duplicateCount(const std::string& in); // helper for tests
#include <array>
#include <string>
size_t duplicateCount(const char* in)
{
std::string data(in);
std::array<int, 26> Count;
Count.fill(0);
for (char i : data) {
if (i - 'a' >= 0 && i - 'a' < 26) {
Count[i - 'a']++;
}
else if (i - 'A' >= 0 && i - 'A' < 26) {
Count[i - 'A']++;
}
}
int ret = 0;
for (int i : Count) {
ret += int(i > 1);
}
return ret;
}
欢迎关注微信公众号:小霍编程