Leetcode 2315: Count Asterisks

grid47
grid47
Exploring patterns and algorithms
Mar 20, 2024 4 min read

You are given a string s, where every two consecutive vertical bars ‘|’ form a pair. Count the number of ‘’ in s, excluding the ‘’ between each pair of ‘|’.
Problem
Approach
Steps
Complexity
Input: The input consists of a string s which contains lowercase English letters, vertical bars '|', and asterisks '*'.
Example: s = '|*a|*b*|c*|'
Constraints:
• 1 <= s.length <= 1000
• s contains only lowercase English letters, '|', and '*' characters.
• s contains an even number of vertical bars '|'.
Output: Return the number of '*' characters in the string, excluding those between the vertical bars '|'.
Example: For s = '|*a|*b*|c*|', the output is 2.
Constraints:
• Only '*' characters outside of the '|' pairs should be counted.
Goal: The goal is to iterate through the string and count the asterisks '*' outside the pairs of vertical bars '|'.
Steps:
• 1. Traverse the string and track when you encounter a vertical bar '|'.
• 2. When a vertical bar is encountered, skip counting asterisks that appear between this bar and the next one.
• 3. Count the asterisks that are outside these pairs.
Goal: The problem constraints ensure that the string is manageable in size and only contains lowercase letters, vertical bars, and asterisks.
Steps:
• The string will always contain an even number of vertical bars.
• Each vertical bar will belong to a pair.
Assumptions:
• The input string contains at least one vertical bar and may contain other characters, including asterisks.
Input: s = '|*a|*b*|c*|'
Explanation: In this example, asterisks inside the vertical bars are excluded. The valid asterisks are those outside the bars, which are counted to give a result of 2.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus