##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/2315.html

# 2315. Count Asterisks

• Difficulty: Easy.
• Related Topics: String.
• Similar Questions: .

## 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 **'*' 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: 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 <= 1000

• s consists of lowercase English letters, vertical bars '|', and asterisks '*'.

• s contains an even number of vertical bars '|'.

## Solution (Java, C++, Python)

• class Solution {
public int countAsterisks(String s) {
int c = 0;
int n = s.length();
int i = 0;
while (i < n) {
if (s.charAt(i) == '|') {
i++;
while (s.charAt(i) != '|') {
i++;
}
}
if (s.charAt(i) == '*') {
c++;
}
i++;
}
return c;
}
}

############

class Solution {
public int countAsterisks(String s) {
int ans = 0;
for (int i = 0, ok = 1; i < s.length(); ++i) {
char c = s.charAt(i);
if (c == '*') {
ans += ok;
} else if (c == '|') {
ok ^= 1;
}
}
return ans;
}
}

• class Solution:
def countAsterisks(self, s: str) -> int:
ans, ok = 0, 1
for c in s:
if c == "*":
ans += ok
elif c == "|":
ok ^= 1
return ans

############

# 2315. Count Asterisks
# https://leetcode.com/problems/count-asterisks/

class Solution:
def countAsterisks(self, s: str) -> int:
A = s.split('|')
n = len(A)
res = 0

for i in range(0, n, 2):
res += A[i].count("*")

return res


• class Solution {
public:
int countAsterisks(string s) {
int ans = 0, ok = 1;
for (char& c : s) {
if (c == '*') {
ans += ok;
} else if (c == '|') {
ok ^= 1;
}
}
return ans;
}
};

• func countAsterisks(s string) (ans int) {
ok := 1
for _, c := range s {
if c == '*' {
ans += ok
} else if c == '|' {
ok ^= 1
}
}
return
}

• function countAsterisks(s: string): number {
let ans = 0;
let ok = 1;
for (const c of s) {
if (c === '*') {
ans += ok;
} else if (c === '|') {
ok ^= 1;
}
}
return ans;
}


• public class Solution {
public int CountAsterisks(string s) {
int ans = 0, ok = 1;
foreach (char c in s) {
if (c == '*') {
ans += ok;
} else if (c == '|') {
ok ^= 1;
}
}
return ans;
}
}

• impl Solution {
pub fn count_asterisks(s: String) -> i32 {
let mut ans = 0;
let mut ok = 1;
for &c in s.as_bytes() {
if c == b'*' {
ans += ok
} else if c == b'|' {
ok ^= 1
}
}
ans
}
}


Explain:

nope.

Complexity:

• Time complexity : O(n).
• Space complexity : O(n).