2315. Count Asterisks LeetCode Solution

In this guide, you will get 2315. Count Asterisks LeetCode Solution with the best time and space complexity. The solution to Count Asterisks problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Count Asterisks solution in C++
  4. Count Asterisks solution in Java
  5. Count Asterisks solution in Python
  6. Additional Resources
2315. Count Asterisks LeetCode Solution image

Problem Statement of 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 '|'.

See also  1720. Decode XORed Array LeetCode Solution

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

2315. Count Asterisks LeetCode Solution in C++

class Solution {
 public:
  int countAsterisks(string s) {
    int ans = 0;
    int bars = 0;

    for (const char c : s) {
      if (c == '|')
        ++bars;
      else if (c == '*' && bars % 2 == 0)
        ++ans;
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

2315. Count Asterisks LeetCode Solution in Java

class Solution {
  public int countAsterisks(String s) {
    int ans = 0;
    int bars = 0;

    for (final char c : s.toCharArray()) {
      if (c == '|')
        ++bars;
      else if (c == '*' && bars % 2 == 0)
        ++ans;
    }

    return ans;
  }
}
// code provided by PROGIEZ

2315. Count Asterisks LeetCode Solution in Python

class Solution:
  def countAsterisks(self, s: str) -> int:
    ans = 0
    bars = 0

    for c in s:
      if c == '|':
        bars += 1
      elif c == '*' and bars % 2 == 0:
        ans += 1

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.