Count Asterisks

To solve this problem, you can use a boolean variable to keep track of whether you are currently between two vertical bars or not. Also, an integer variable is needed to count the number of asterisks. You iterate over the characters of the string. When you encounter a vertical bar, you flip the boolean variable to indicate that you are now either inside or outside a pair of vertical bars. If you encounter an asterisk and you are not between vertical bars, you increase the count.

Python solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution:
    def countAsterisks(self, s: str) -> int:
        count = 0
        between_bars = False
        for char in s:
            if char == '|':
                between_bars = not between_bars
            elif char == '*' and not between_bars:
                count += 1
        return count

In this function, count is used to keep track of the number of asterisks encountered outside of vertical bars. between_bars is a boolean that is flipped every time a vertical bar is encountered, indicating whether we are currently between two vertical bars or not. We then loop over the characters in the string. If we encounter a vertical bar, we flip between_bars. If we encounter an asterisk and we are not currently between vertical bars (between_bars is False), we increase count. Finally, we return count, which is the number of asterisks encountered outside of vertical bars.