Count Asterisks - 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: Each '|' will belong to exactly one pair.

Input & Output

Example 1 — Basic Case
$ Input: s = "l*e*et|*c|o**de|"
Output: 2
💡 Note: The pairs are formed by 1st-2nd '|' and 3rd-4th '|'. Excluding asterisks between pairs, we have "l*e*et" and "o**de" outside pairs, containing 2+2=4 asterisks total. Wait, let me recalculate: "l*e*et" has 2 asterisks, and after the last pair we have nothing. So it's just 2.
Example 2 — Multiple Pairs
$ Input: s = "iamprogrammer"
Output: 0
💡 Note: No vertical bars means no pairs, but also no asterisks to count. Answer is 0.
Example 3 — All Outside
$ Input: s = "yo|uar|e**|awesome|"
Output: 2
💡 Note: Pairs are 1st-2nd '|' and 3rd-4th '|'. Outside sections are "yo" and "e**" and empty string after last '|'. Only "e**" has asterisks (2 of them).

Constraints

  • 1 ≤ s.length ≤ 1000
  • s consists of lowercase English letters, '*', and '|'.
  • s contains an even number of '|'.

Visualization

Tap to expand
Count Asterisks Problem OverviewInput: l*e*et|code|*l*e*etcode*OUTSIDE (count)INSIDE (skip)OUTSIDE (count)||Vertical bars form pairs that create hidden zones2 asterisks ✓0 asterisks (hidden)1 asterisk ✓Result: 2 + 0 + 1 = 3 asterisks
Understanding the Visualization
1
Input
String with asterisks and vertical bar pairs
2
Process
Identify inside/outside zones using bar pairs
3
Output
Count of asterisks in outside zones only
Key Takeaway
🎯 Key Insight: Pairs of vertical bars create alternating inside/outside zones - toggle state on each '|'
Asked in
Amazon 15 Microsoft 8
12.5K Views
Medium Frequency
~10 min Avg. Time
450 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen