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 substrings[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 arrayanswerwhereanswer[i]is the answer to theithquery.
Input: s ="**|**|***|", queries =[[2,5],[5,9]]Output: [2,3]Explanation:
- queries[0] has two plates between candles.- queries[1] has three plates between candles.
Input: s ="***|**|*****|**||**|*", queries =[[1,17],[4,5],[14,17],[5,11],[15,16]]Output: [9,0,0,0,0]Explanation:
- queries[0] has nine plates between candles.- The other queries have zero plates between candles.
For each query, we need to efficiently count the number of plates ('*') that are between two candles (’|’) within the substring. Since both s and queries can be large, a brute-force approach is too slow. We can use a prefix sum array prefix (where prefix[i] counts plates up to index i-1) and precompute the nearest candle indices to the left and right for each position as arrays left and right, allowing O(1) answers per query after O(n) preprocessing.
Precompute a prefix-sum array prefix of plates where prefix[i] = number of '*' in s[0..i-1].
For each position i, precompute left[i] = index of the nearest candle at or before i (or -1 if none) and right[i] = index of the nearest candle at or after i (or n if none).
For each query [l, r]:
Let left_candle = right[l] (first candle at or after l).
Let right_candle = left[r] (last candle at or before r).
If left_candle < right_candle, answer is prefix[right_candle] - prefix[left_candle]; otherwise answer is 0.
#include<vector>#include<string>usingnamespace std;
classSolution {
public: vector<int> platesBetweenCandles(string s, vector<vector<int>>& queries) {
int n = s.size();
vector<int> prefix(n +1, 0);
for (int i =0; i < n; ++i)
prefix[i +1] = prefix[i] + (s[i] =='*'?1:0);
vector<int> left(n, -1), right(n, n);
for (int i =0, last =-1; i < n; ++i) {
if (s[i] =='|') last = i;
left[i] = last;
}
for (int i = n -1, last = n; i >=0; --i) {
if (s[i] =='|') last = i;
right[i] = last;
}
vector<int> res;
for (auto& q : queries) {
int l = right[q[0]], r = left[q[1]];
if (l < r)
res.push_back(prefix[r] - prefix[l]);
else res.push_back(0);
}
return res;
}
};
import java.util.*;
classSolution {
publicint[]platesBetweenCandles(String s, int[][] queries) {
int n = s.length();
int[] prefix =newint[n + 1];
for (int i = 0; i < n; i++)
prefix[i + 1]= prefix[i]+ (s.charAt(i) =='*'? 1 : 0);
int[] left =newint[n], right =newint[n];
int last =-1;
for (int i = 0; i < n; i++) {
if (s.charAt(i) =='|') last = i;
left[i]= last;
}
last = n;
for (int i = n - 1; i >= 0; i--) {
if (s.charAt(i) =='|') last = i;
right[i]= last;
}
int[] res =newint[queries.length];
for (int i = 0; i < queries.length; i++) {
int l = right[queries[i][0]], r = left[queries[i][1]];
if (l < r)
res[i]= prefix[r]- prefix[l];
else res[i]= 0;
}
return res;
}
}
classSolution {
funplatesBetweenCandles(s: String, queries: Array<IntArray>): IntArray {
val n = s.length
val prefix = IntArray(n + 1)
for (i in0 until n)
prefix[i + 1] = prefix[i] + if (s[i] =='*') 1else0val left = IntArray(n)
val right = IntArray(n)
var last = -1for (i in0 until n) {
if (s[i] =='|') last = i
left[i] = last
}
last = n
for (i in n - 1 downTo 0) {
if (s[i] =='|') last = i
right[i] = last
}
return queries.map {
val l = right[it[0]]
val r = left[it[1]]
if (l < r) prefix[r] - prefix[l] else0 }.toIntArray()
}
}
classSolution:
defplatesBetweenCandles(self, s: str, queries: list[list[int]]) -> list[int]:
n = len(s)
prefix = [0] * (n +1)
for i in range(n):
prefix[i +1] = prefix[i] + (s[i] =='*')
left = [-1] * n
right = [n] * n
last =-1for i in range(n):
if s[i] =='|':
last = i
left[i] = last
last = n
for i in range(n -1, -1, -1):
if s[i] =='|':
last = i
right[i] = last
res = []
for l, r in queries:
l_candle = right[l]
r_candle = left[r]
if l_candle < r_candle:
res.append(prefix[r_candle] - prefix[l_candle])
else:
res.append(0)
return res
impl Solution {
pubfnplates_between_candles(s: String, queries: Vec<Vec<i32>>) -> Vec<i32> {
let n = s.len();
let s = s.as_bytes();
letmut prefix =vec![0; n +1];
for i in0..n {
prefix[i +1] = prefix[i] +if s[i] ==b'*' { 1 } else { 0 };
}
letmut left =vec![-1; n];
letmut right =vec![n asi32; n];
letmut last =-1;
for i in0..n {
if s[i] ==b'|' { last = i asi32; }
left[i] = last;
}
last = n asi32;
for i in (0..n).rev() {
if s[i] ==b'|' { last = i asi32; }
right[i] = last;
}
queries.iter().map(|q| {
let l = right[q[0] asusize];
let r = left[q[1] asusize];
if l < r {
prefix[r asusize] - prefix[l asusize]
} else { 0 }
}).collect()
}
}