We are required to count the number of asterisks between two pairs of |
. Think of a pair of |
as a wall within which you cannot see anything, and you can see everything between two such walls.
Well, we are just applying the same logic. We have a boolean flag variable that toggles to the moment we enter a wall (pair of |
). When that boolean flag is off, we don’t capture anything. Once it is on, we read for asterisks and return the total.
Code:
Python
Big O Analysis
-
Runtime
The runtime complexity here is
O(N)
as since we would be iterating the array atleast once. -
Memory
The memory usage is
O(N)
since we use a stack to keep track of characters.
— A