Plates Between Candles

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, 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.

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

Example 1
Inputs = "**|**|***|", queries = [[2,5],[5,9]]
Output[2,3]
queries[0] has two plates between candles, and queries[1] has three plates between candles.
Example 2
Inputs = "***|**|*****|**||**|*", queries = [[1,17],[4,5],[14,17],[5,11],[15,16]]
Output[9,0,0,0,0]
queries[0] has nine plates between candles, and the other queries have zero plates between candles.

Constraints

  • 3 <= s.length <= 10^5
  • s consists of '*' and '|' characters.
  • 1 <= queries.length <= 10^5
  • queries[i].length == 2
  • 0 <= lefti <= righti < s.length

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate