Count Asterisks LeetCode solution in C++

1
/*
2
1.) Maintain a counter to store the '|'
3
2.) After every even no. of '|' count the asteriks.
4
*/
5
class Solution {
6
public:
7
int countAsterisks(string s) {
8
int ans = 0;
9

10
// count of no. of '|'
11
int cnt = 0;
12

13
for (int i = 0; i < s.size(); i++) {
14
// if count is even then count the asteriks
15
if (cnt % 2 == 0) {
16
if (s[i] == '*') ans++;
17
}
18
if (s[i] == '|') cnt++;
19
}
20
return ans;
21
}
22
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0