[LeetCode] Count Asterisks

2315. Count Asterisks

You are given a string s, where every two consecutive vertical bars ‘|’ are grouped into a pair. In other words, the 1st and 2nd ‘|’ make a pair, the 3rd and 4th ‘|’ make a pair, and so /forth.

Return the number of '*' in s, excluding the '*' between each pair of ‘|’.

Note that each ‘|’ will belong to exactly one pair.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
int countAsterisks(string s) {
int res = 0, now = 0;
for(auto& ch : s) {
if(ch == '|') now++;
else if(ch == '*' and (now & 1) == 0) res++;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/06/26/PS/LeetCode/count-asterisks/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.