Count Asterisks

easy string

Problem

You are given a string s, where every two consecutive vertical bars '|' are grouped into a pair (the 1st and 2nd bars are a pair, the 3rd and 4th are a pair, and so on). Return the number of '*' characters in s, excluding the asterisks that lie between any pair of '|'. The number of bars is guaranteed to be even.

Inputs = "l|*e*et|c**o|*de|"
Output2
Stars inside the bar pairs are ignored; only the two '*' in "c**o" count.

def count_asterisks(s):
    inside = False
    count = 0
    for ch in s:
        if ch == "|":
            inside = not inside
        elif ch == "*" and not inside:
            count += 1
    return count
function countAsterisks(s) {
  let inside = false;
  let count = 0;
  for (const ch of s) {
    if (ch === "|") {
      inside = !inside;
    } else if (ch === "*" && !inside) {
      count++;
    }
  }
  return count;
}
class Solution {
    public int countAsterisks(String s) {
        boolean inside = false;
        int count = 0;
        for (char ch : s.toCharArray()) {
            if (ch == '|') inside = !inside;
            else if (ch == '*' && !inside) count++;
        }
        return count;
    }
}
int countAsterisks(string s) {
    bool inside = false;
    int count = 0;
    for (char ch : s) {
        if (ch == '|') inside = !inside;
        else if (ch == '*' && !inside) count++;
    }
    return count;
}
Time: O(n) Space: O(1)