answer
of the same length as s
with all elements set to a large number (e.g., s.length
).prev
to a large negative value to represent that we haven't seen the character c
yet.s
from left to right. For each index i
, if s[i]
is c
, update prev
to i
. Then set answer[i]
to i - prev
.prev
to a large positive value to represent that we haven't seen the character c
from the right.s
from right to left. For each index i
, if s[i]
is c
, update prev
to i
. Then update answer[i]
to the minimum of answer[i]
and prev - i
.answer
array.