Count Asterisks Problem & Solution
See the count asterisks problem on LeetCode.
C++ Solution
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,fma")
#pragma GCC ivdep
static const int _=[](){ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);return 0;}();
class Solution {
public:
int countAsterisks(string s) {
int pipes = 0, asterisks = 0, total = 0;
for (auto c : s) {
if (c == '|') {
if (++pipes % 2 == 1) {
total += asterisks;
}
asterisks = 0;
} else if (c == '*') {
++asterisks;
}
}
return total + asterisks;
}
};