Plates Between Candles - Problem

There is a long table with a line of plates and candles arranged on top of it. You are given a 0-indexed string s consisting of characters '*' and '|' only, where a '*' represents a plate and a '|' represents a candle.

You are also given a 0-indexed 2D integer array queries where queries[i] = [lefti, righti] denotes the substring s[lefti...righti] (inclusive). For each query, you need to find the number of plates between candles that are in the substring. A plate is considered between candles if there is at least one candle to its left and at least one candle to its right in the substring.

For example, s = "||**||**|*", and a query [3, 8] denotes the substring "*||**|". The number of plates between candles in this substring is 2, as each of the two plates has at least one candle in the substring to its left and right.

Return an integer array answer where answer[i] is the answer to the ith query.

Input & Output

Example 1 — Basic Case
$ Input: s = "**|**|***|", queries = [[2,5],[5,9]]
Output: [2,3]
💡 Note: Query [2,5]: substring is "|**|", plates at indices 3,4 are between candles at 2,5. Query [5,9]: substring is "|***|", plates at indices 6,7,8 are between candles at 5,9.
Example 2 — No Candles
$ Input: s = "***", queries = [[0,2]]
Output: [0]
💡 Note: Query [0,2]: substring is "***", no candles exist so no plates can be between candles.
Example 3 — Single Candle
$ Input: s = "|**|", queries = [[0,3]]
Output: [2]
💡 Note: Query [0,3]: substring is "|**|", plates at indices 1,2 are between candles at 0,3.

Constraints

  • 3 ≤ s.length ≤ 105
  • s consists of '*' and '|' characters only
  • 1 ≤ queries.length ≤ 105
  • queries[i].length == 2
  • 0 ≤ lefti ≤ righti < s.length

Visualization

Tap to expand
Plates Between Candles INPUT String s: 0 1 2 3 4 5 6 7 8 9 * * | * * | * * * | = Plate (*) = Candle (|) Queries: [2, 5] [5, 9] Query 1: indices 2-5 |**| Query 2: indices 5-9 |***| ALGORITHM STEPS 1 Precompute Arrays leftCandle[], rightCandle[] prefix[] for plate count 2 For Each Query [L,R] Find first candle from L Find last candle from R 3 Calculate Result prefix[right] - prefix[left] 4 Return Answer Plates between candles Precomputed Arrays: leftCandle: -1 -1 2 2 2 5 5 5 5 9 rightCandle: 2 2 2 5 5 5 9 9 9 9 prefix: 1 2 2 3 4 4 5 6 7 7 FINAL RESULT Query 1: [2, 5] Substring: "|**|" leftCandle = rightCandle[2] = 2 rightCandle = leftCandle[5] = 5 prefix[5] - prefix[2] = 4 - 2 = 2 Result: 2 plates | * * | 2 plates between candles Query 2: [5, 9] Substring: "|***|" leftCandle = rightCandle[5] = 5 rightCandle = leftCandle[9] = 9 prefix[9] - prefix[5] = 7 - 4 = 3 Result: 3 plates |
Asked in
Google 15 Amazon 12 Microsoft 8 Facebook 6
28.0K Views
Medium Frequency
~25 min Avg. Time
890 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
Algorithm Visualization
Pinch to zoom • Tap outside to close
Test Cases
0 passed
0 failed
3 pending

Select Compiler

Choose a programming language

Compiler list would appear here...