cpp: add «1915. Number of Wonderful Substrings»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
a55a560f58
commit
4e873f87b0
1 changed files with 29 additions and 0 deletions
29
cpp/number-of-wonderful-substrings.cpp
Normal file
29
cpp/number-of-wonderful-substrings.cpp
Normal file
|
@ -0,0 +1,29 @@
|
|||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
class Solution {
|
||||
static const std::uint32_t BIT = 1;
|
||||
|
||||
public:
|
||||
long long wonderfulSubstrings(const std::string &word) {
|
||||
long long result = 0;
|
||||
|
||||
std::unordered_map<std::uint32_t, long long> freqs;
|
||||
freqs[0] = 1;
|
||||
|
||||
std::uint32_t mask = 0;
|
||||
for (char c : word) {
|
||||
mask ^= BIT << (c - 'a');
|
||||
|
||||
result += freqs[mask];
|
||||
++freqs[mask];
|
||||
|
||||
for (auto odd_c = 0; odd_c < 10; ++odd_c) {
|
||||
result += freqs[mask ^ (BIT << odd_c)];
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
};
|
Loading…
Reference in a new issue