2055. Plates Between Candles - Detailed Explanation
Problem Statement
You are given a string s consisting of characters '*'
(plates) and '|'
(candles), and an array of queries where each query is a pair [left, right]
. For each query, consider the substring s[left…right] (inclusive). You want to count the number of plates ('*'
) between the leftmost and rightmost candles ('|'
) within that substring. Return an array of answers, one per query.
Input
- s: a string of length n, made up of
'*'
and'|'
- queries: a list of q pairs
[left, right]
, with 0 ≤ left ≤ right < n
Output
- A list of q integers, where the _i_th integer is the count of plates between candles in s[queries[i][0]…queries[i][1]]
Examples
Example 1
s = "**|**|***|"
queries = [[2,5],[5,9]]
- For
[2,5]
, substring ="|**|"
. Plates between the two|
are 2. - For
[5,9]
, substring ="|***|"
. Plates between are 3.
Output:[2,3]
Example 2
s = "***|**|*****|**||**|*"
queries = [[1,17],[4,5],[14,17],[5,11],[15,16]]
Output: [9,0,0,3,0]
Constraints
- 1 ≤ n, q ≤ 10⁵
- s[i] is either
'*'
or'|'
- 0 ≤ left ≤ right < n
Brute‑Force Approach
- For each query
[l,r]
, extract substring t = s[l…r]. - Scan t to find the index of the first
'|'
(let’s call it a) and the last'|'
(call it b). - If a < b, count the
'*'
in t[a+1…b−1]; otherwise answer is 0. - Repeat for all queries.
Time Complexity: O(q × n) in the worst case (each query may scan up to n characters)
Space Complexity: O(1) extra (ignoring output array)
Optimal Approach with Prefix Sums and Nearest‑Candle Pointers
To answer queries in O(1), preprocess the string once:
-
Prefix‑sum of plates
- Let plates[i] = number of
'*'
in s[0…i−1]. - Build in one pass:
plates[0] = 0 for i in [1…n]: plates[i] = plates[i−1] + (s[i−1]=='*'?1:0)
- Let plates[i] = number of
-
Nearest candle to the left
- leftCandle[i] = index ≤ i of the closest
'|'
; or-1
if none. - Build by tracking last seen candle index.
- leftCandle[i] = index ≤ i of the closest
-
Nearest candle to the right
- rightCandle[i] = index ≥ i of the closest
'|'
; orn
if none. - Build by scanning from right to left.
- rightCandle[i] = index ≥ i of the closest
With these, for each query [l,r]
:
- Let a = rightCandle[l], b = leftCandle[r].
- If a < b, plates between = plates[b] − plates[a]; otherwise 0.
Building Helpers in One Pass Each
plates = int[n+1] leftCandle = int[n] rightCandle = int[n] # prefix plates plates[0] = 0 for i in 1…n: plates[i] = plates[i-1] + (s[i-1]=='*'?1:0) # leftCandle last = -1 for i in 0…n-1: if s[i]=='|': last = i leftCandle[i] = last # rightCandle next = n for i in n-1…0: if s[i]=='|': next = i rightCandle[i] = next
Query in O(1)
for each [l,r]: a = rightCandle[l] b = leftCandle[r] if a < b: answer = plates[b] - plates[a] else answer = 0
Overall Time Complexity: O(n + q)
Space Complexity: O(n)
Python Code
Java Code
Step‑by‑Step Walkthrough
Take s = "*||*|", queries = [[2,5],[5,9]]
- plates builds to
[0,1,1,2,2,3,4,5,5]
- leftCandle =
[-1,1,1,1,4,4,4,7,7]
- rightCandle =
[1,1,3,3,4,7,7,7,8]
Query [2,5]:
- a = rightCandle[2] = 3
- b = leftCandle[5] = 4
- plates[4] − plates[3] = 2 − 2 = 0?
Wait, check indices carefully: plates index shifted by one:
Actually plates at positions of candles: plate count up to index
plates[b] = plates[4] = 2, plates[a] = plates[3] = 2 → difference = 0?
That seems off because answer should be 2. Real calculation uses plates[b] − plates[a+1]?
Better: define plates so plates[i] = plates up to but not including i. With a and b candle positions, plates between = plates[b] − plates[a+1].
Alternatively shift accordingly; final code uses correct offset.
Common Mistakes
- Off‑by‑one when using prefix array index
- Not handling queries with no candles in range (a or b invalid)
- Forgetting to preprocess both left and right nearest candles
Edge Cases
- No candle in entire string → all answers 0
- Queries where left and right pointers land on same candle → 0 plates
- String of only plates or only candles
Alternative Variations
- Max Plates Between Candles: find the query range that maximizes plates
- Dynamic Updates: if
s
changes over time, support queries online (segment tree)
Related Problems
GET YOUR FREE
Coding Questions Catalog