Ctrl K

Stars Outside the Fences

Easy
The string text contains lowercase English letters, asterisks * and vertical bars |. The bars pair up in order: the first with the second, the third with the fourth, and so on. Everything between the two bars of a pair is fenced off. If the number of bars is odd, the last bar has no partner, and everything from it to the end of the string is fenced off. Return the number of asterisks * that are not fenced off by any pair.
Example 1
text="a*b|**|c*" → 2

The two stars inside the first pair of | are skipped; the ones after a and after c remain, 2 in total.

Example 2
text="|*|*|*|" → 1

The bars pair up as 1–2 and 3–4. Each pair fences one star, and the single star between the pairs counts.

Example 3
text="stars" → 0

There are no stars at all, so the answer is 0.

Constraints
  • 1 ≤ len(text) ≤ 1000
  • text consists of lowercase English letters, * and |
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsValue of a Roman Numeral EasyShared Start of Words EasyBracket Balance EasyWhere the Fragment Starts EasyLength of the Final Word Easy
loading Python…
Input
text="a*b|**|c*"
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 107 tests on the server, hidden ones included.