count distinct characters in a string c++ code example
Example 1: count a character in a string c++
count(str.begin(), str.end(), 'e')
Example 2: count occurrences of character in string c++
std::string s = "a_b_c";
size_t n = std::count(s.begin(), s.end(), '_'); // n=2
Example 3: finding no of unique characters in a string c++
int countDistinct(string s)
{
unordered_map<char, int> m;
for (int i = 0; i < s.length(); i++) {
m[s[i]]++;
}
return m.size();
}