You are given a string `s`

, where every **two** consecutive vertical bars `'|'`

are grouped into a **pair**. In other words, the 1^{st} and 2^{nd} `'|'`

make a pair, the 3^{rd} and 4^{th} `'|'`

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.

**Example 1:**

Input:s = "l|*e*et|c**o|*de|"Output:2Explanation:The considered characters are underlined: "l|*e*et|c**o|*de|". The characters between the first and second '|' are excluded from the answer. Also, the characters between the third and fourth '|' are excluded from the answer. There are 2 asterisks considered. Therefore, we return 2.

**Example 2:**

Input:s = "iamprogrammer"Output:0Explanation:In this example, there are no asterisks in s. Therefore, we return 0.

**Example 3:**

Input:s = "yo|uar|e**|b|e***au|tifu|l"Output:5Explanation:The considered characters are underlined: "yo|uar|e**|b|e***au|tifu|l". There are 5 asterisks considered. Therefore, we return 5.

**Constraints:**

`1 <= s.length <= 1000`

`s`

consists of lowercase English letters, vertical bars`'|'`

, and asterisks`'*'`

.`s`

contains an**even**number of vertical bars`'|'`

.

**Solution: Counting**

Count the number of bars so far, and only count ‘*’ when there are even number of bars on the left.

Time complexity: O(n)

Space complexity: O(1)

## C++

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Author: Huahua class Solution { public: int countAsterisks(string s) { int bars = 0; int ans = 0; for (char c : s) { if (c == '*' && bars % 2 == 0) ++ans; if (c == '|') ++bars; } return ans; } }; |