2
// O(sLen + queries.length) time, O(sLen) space3
public int[] platesBetweenCandles(String s, int[][] queries) {5
// cumulative number of plates from the left6
int[] numberOfPlates = new int[sLen + 1];7
for (int i = 0; i < sLen; i++) {8
numberOfPlates[i + 1] = numberOfPlates[i] + (s.charAt(i) == '*' ? 1 : 0);10
// closest candle to the left11
int[] candleToTheLeft = new int[sLen];13
for (int i = 0; i < sLen; i++) {14
if (s.charAt(i) == '|') {17
candleToTheLeft[i] = cand;19
// closest candle to the right20
int[] candleToTheRight = new int[sLen];22
for (int i = sLen - 1; i >= 0; i--) {23
if (s.charAt(i) == '|') {26
candleToTheRight[i] = cand;28
// for each query - count the number of plates between closest candles29
int[] res = new int[queries.length];30
for (int i = 0; i < queries.length; i++) {31
int left = candleToTheRight[queries[i][0]];32
int right = candleToTheLeft[queries[i][1]];33
if (left == -1 || right == -1 || left >= right) {36
res[i] = numberOfPlates[right + 1] - numberOfPlates[left];