{x}
blog image

Count Asterisks

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 Explanation for LeetCode 2315: Count Asterisks

This problem requires counting asterisks in a string, excluding those between pairs of vertical bars (|). The solution uses a simple iterative approach with a flag to track whether we are currently inside or outside a pair of bars.

Approach

The core idea is to iterate through the string character by character. We maintain a boolean variable ok (or its integer equivalent) which indicates whether we should count asterisks currently. Initially, ok is true (or 1).

  • When a | is encountered: We toggle the value of ok. If ok was true, it becomes false (we're now inside a bar pair), and vice-versa. This effectively switches between counting and not counting asterisks.

  • When a * is encountered: If ok is true, we increment the asterisk counter ans.

  • Other characters: We ignore them.

After iterating through the entire string, ans will hold the desired count.

Time and Space Complexity

  • Time Complexity: O(n), where n is the length of the input string. We iterate through the string once.

  • Space Complexity: O(1). We use only a few constant extra variables (ans and ok).

Code Implementation (Python)

class Solution:
    def countAsterisks(self, s: str) -> int:
        ans, ok = 0, 1  # ans: asterisk counter, ok: flag (1 for counting, 0 for not)
        for c in s:
            if c == "*":
                ans += ok  # Add to count only if ok is true (1)
            elif c == "|":
                ok ^= 1     # Toggle ok (0 XOR 1 = 1, 1 XOR 1 = 0)
        return ans
 

Code Implementation (Java)

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

The implementations in other languages (C++, Go, TypeScript, Rust, C#, C) follow a similar structure, adapting the syntax appropriately. The core logic remains the same: iterating through the string, toggling a flag based on vertical bars, and counting asterisks conditionally.