Skip to main content
Back to problems
#2315
Easy Algorithms

Count asterisks

String
83.3% acceptance
Feb 25, 2026
681
118
You are given a string s, where every two consecutive vertical bars '|' are grouped into a pair. Return the number of '*' in s, excluding the '*' between each pair of '|'.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn count_asterisks(s: String) -> i32 {
    let mut inside = false;
    let mut count = 0;
    for c in s.chars() {
      match c {
        '|' => inside = !inside,
        '*' if !inside => count += 1,
        _ => {}
      }
    }
    count
  }
}