Count Asterisks
EasyUpdated: Aug 2, 2025
Practice on:
Problem
You are given a string s, where every two consecutive vertical bars
'|' are grouped into a pair. In other words, the 1st and 2nd '|' make a pair, the 3rd and 4th '|' make a pair, and so forth.
Return the number of'*'ins _,excluding the _'*'between each pair of'|'.
Note that each '|' will belong to exactly one pair.
Examples
Example 1
Input: s = "l|*e*et|c**o|*de|"
Output: 2
Explanation: 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: 0
Explanation: 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: 5
Explanation: 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 <= 1000sconsists of lowercase English letters, vertical bars'|', and asterisks'*'.scontains an even number of vertical bars'|'.
Solution
Method 1 – Toggle State with Counter 1
Intuition
We can keep a flag to track whether we are inside a pair of vertical bars. We only count '*' when we are outside any such pair. Every time we see a '|', we toggle the flag.
Approach
- Initialize a flag (inside) to False and a counter (ans) to 0.
- Iterate through each character in the string:
- If the character is '|', toggle the inside flag.
- If the character is '*' and inside is False, increment the counter.
- Return the counter.
Code
C++
class Solution {
public:
int countAsterisks(string s) {
int ans = 0, inside = 0;
for (char c : s) {
if (c == '|') inside ^= 1;
else if (c == '*' && !inside) ans++;
}
return ans;
}
};
Go
func countAsterisks(s string) int {
ans, inside := 0, false
for _, c := range s {
if c == '|' {
inside = !inside
} else if c == '*' && !inside {
ans++
}
}
return ans
}
Java
class Solution {
public int countAsterisks(String s) {
int ans = 0;
boolean inside = false;
for (char c : s.toCharArray()) {
if (c == '|') inside = !inside;
else if (c == '*' && !inside) ans++;
}
return ans;
}
}
Kotlin
class Solution {
fun countAsterisks(s: String): Int {
var ans = 0
var inside = false
for (c in s) {
if (c == '|') inside = !inside
else if (c == '*' && !inside) ans++
}
return ans
}
}
Python
class Solution:
def countAsterisks(self, s: str) -> int:
ans = 0
inside = False
for c in s:
if c == '|':
inside = not inside
elif c == '*' and not inside:
ans += 1
return ans
Rust
impl Solution {
pub fn count_asterisks(s: String) -> i32 {
let mut ans = 0;
let mut inside = false;
for c in s.chars() {
if c == '|' {
inside = !inside;
} else if c == '*' && !inside {
ans += 1;
}
}
ans
}
}
TypeScript
class Solution {
countAsterisks(s: string): number {
let ans = 0, inside = false;
for (const c of s) {
if (c === '|') inside = !inside;
else if (c === '*' && !inside) ans++;
}
return ans;
}
}
Complexity
- ⏰ Time complexity:
O(n), where n is the length of s, as we scan the string once. - 🧺 Space complexity:
O(1), as only a constant amount of space is used.