class Solution:
def platesBetweenCandles(self, s: str, queries) :
n = len(s)
preSum = [0] * (n+1) # preSum[i+1] 表示 s[:i] 中盘子的个数
left = [0] * n # left[i] 表示 i 左侧最近蜡烛位置
candleIdx = -1
for i, ch in enumerate(s):
if ch == '|':
candleIdx = i
preSum[i+1] = preSum[i]
else:
preSum[i+1] = preSum[i]+1
left[i] = candleIdx
right = [0] * n
candleIdx = n
for i in range(n-1, -1, -1):
if s[i] == '|':
candleIdx = i
right[i] = candleIdx
res = [0] * len(queries)
for i, que in enumerate(queries):
leftIdx, rightIdx = right[que[0]], left[que[1]]
if leftIdx < rightIdx:
res[i] = preSum[rightIdx+1]-preSum[leftIdx+1]
return res
11-21
576
11-18
557