class Solution {
public int[] shortestToChar(String S, char C) {
int n = S.length();
int prev = Integer.MIN_VALUE/2;
int[] store = new int[n];
for(int i = 0; i < n; i++) {
if(S.charAt(i) == C) {
store[i] = 0;
prev = i;
}
else
store[i] = i - prev;
}
prev = Integer.MAX_VALUE/2;
for(int i = n-1; i >= 0; i--) {
if(S.charAt(i) == C)
prev = i;
else
store[i] = Math.min(store[i], prev - i);
}
return store;
}
}
Categories