Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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.
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.
Example 1:
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.
Example 2:
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.
Constraints:
3 <= s.length <= 105s consists of '*' and '|' characters.1 <= queries.length <= 105queries[i].length == 20 <= lefti <= righti < s.lengthProblem summary: 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
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search
"**|**|***|" [[2,5],[5,9]]
"***|**|*****|**||**|*" [[1,17],[4,5],[14,17],[5,11],[15,16]]
find-first-and-last-position-of-element-in-sorted-array)can-make-palindrome-from-substring)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2055: Plates Between Candles
class Solution {
public int[] platesBetweenCandles(String s, int[][] queries) {
int n = s.length();
int[] presum = new int[n + 1];
for (int i = 0; i < n; ++i) {
presum[i + 1] = presum[i] + (s.charAt(i) == '*' ? 1 : 0);
}
int[] left = new int[n];
int[] right = new int[n];
for (int i = 0, l = -1; i < n; ++i) {
if (s.charAt(i) == '|') {
l = i;
}
left[i] = l;
}
for (int i = n - 1, r = -1; i >= 0; --i) {
if (s.charAt(i) == '|') {
r = i;
}
right[i] = r;
}
int[] ans = new int[queries.length];
for (int k = 0; k < queries.length; ++k) {
int i = right[queries[k][0]];
int j = left[queries[k][1]];
if (i >= 0 && j >= 0 && i < j) {
ans[k] = presum[j] - presum[i + 1];
}
}
return ans;
}
}
// Accepted solution for LeetCode #2055: Plates Between Candles
func platesBetweenCandles(s string, queries [][]int) []int {
n := len(s)
presum := make([]int, n+1)
for i := range s {
if s[i] == '*' {
presum[i+1] = 1
}
presum[i+1] += presum[i]
}
left, right := make([]int, n), make([]int, n)
for i, l := 0, -1; i < n; i++ {
if s[i] == '|' {
l = i
}
left[i] = l
}
for i, r := n-1, -1; i >= 0; i-- {
if s[i] == '|' {
r = i
}
right[i] = r
}
ans := make([]int, len(queries))
for k, q := range queries {
i, j := right[q[0]], left[q[1]]
if i >= 0 && j >= 0 && i < j {
ans[k] = presum[j] - presum[i+1]
}
}
return ans
}
# Accepted solution for LeetCode #2055: Plates Between Candles
class Solution:
def platesBetweenCandles(self, s: str, queries: List[List[int]]) -> List[int]:
n = len(s)
presum = [0] * (n + 1)
for i, c in enumerate(s):
presum[i + 1] = presum[i] + (c == '*')
left, right = [0] * n, [0] * n
l = r = -1
for i, c in enumerate(s):
if c == '|':
l = i
left[i] = l
for i in range(n - 1, -1, -1):
if s[i] == '|':
r = i
right[i] = r
ans = [0] * len(queries)
for k, (l, r) in enumerate(queries):
i, j = right[l], left[r]
if i >= 0 and j >= 0 and i < j:
ans[k] = presum[j] - presum[i + 1]
return ans
// Accepted solution for LeetCode #2055: Plates Between Candles
/**
* [2055] 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, 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 "*||<u>**</u>|". 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 i^th query.
*
* Example 1:
* <img alt="ex-1" src="https://assets.leetcode.com/uploads/2021/10/04/ex-1.png" style="width: 400px; height: 134px;" />
* 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.
*
* Example 2:
* <img alt="ex-2" src="https://assets.leetcode.com/uploads/2021/10/04/ex-2.png" style="width: 600px; height: 193px;" />
* 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.
*
*
* 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
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/plates-between-candles/
// discuss: https://leetcode.com/problems/plates-between-candles/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn plates_between_candles(s: String, queries: Vec<Vec<i32>>) -> Vec<i32> {
vec![]
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2055_example_1() {
let s = "**|**|***|".to_string();
let queries = vec![vec![2, 5], vec![5, 9]];
let result = vec![2, 3];
assert_eq!(Solution::plates_between_candles(s, queries), result);
}
#[test]
#[ignore]
fn test_2055_example_2() {
let s = "***|**|*****|**||**|*".to_string();
let queries = vec![
vec![1, 17],
vec![4, 5],
vec![14, 17],
vec![5, 11],
vec![15, 16],
];
let result = vec![9, 0, 0, 0, 0];
assert_eq!(Solution::plates_between_candles(s, queries), result);
}
}
// Accepted solution for LeetCode #2055: Plates Between Candles
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #2055: Plates Between Candles
// class Solution {
// public int[] platesBetweenCandles(String s, int[][] queries) {
// int n = s.length();
// int[] presum = new int[n + 1];
// for (int i = 0; i < n; ++i) {
// presum[i + 1] = presum[i] + (s.charAt(i) == '*' ? 1 : 0);
// }
// int[] left = new int[n];
// int[] right = new int[n];
// for (int i = 0, l = -1; i < n; ++i) {
// if (s.charAt(i) == '|') {
// l = i;
// }
// left[i] = l;
// }
// for (int i = n - 1, r = -1; i >= 0; --i) {
// if (s.charAt(i) == '|') {
// r = i;
// }
// right[i] = r;
// }
// int[] ans = new int[queries.length];
// for (int k = 0; k < queries.length; ++k) {
// int i = right[queries[k][0]];
// int j = left[queries[k][1]];
// if (i >= 0 && j >= 0 && i < j) {
// ans[k] = presum[j] - presum[i + 1];
// }
// }
// return ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.