Given a string s and a character c that occurs in s, return an array of integers answer where answer.length == s.length and answer[i] is the distance from index i to the closest occurrence of character c in s.

The distance between two indices i and j is abs(i - j), where abs is the absolute value function.

&nbsp;
Example 1:

Input: s = "loveleetcode", c = "e"
Output: [3,2,1,0,1,0,0,1,2,2,1,0]
Explanation: The character 'e' appears at indices 3, 5, 6, and 11 (0-indexed).
The closest occurrence of 'e' for index 0 is at index 3, so the distance is abs(0 - 3) = 3.
The closest occurrence of 'e' for index 1 is at index 3, so the distance is abs(1 - 3) = 2.
For index 4, there is a tie between the 'e' at index 3 and the 'e' at index 5, but the distance is still the same: abs(4 - 3) == abs(4 - 5) = 1.
The closest occurrence of 'e' for index 8 is at index 6, so the distance is abs(8 - 6) = 2.


Example 2:

Input: s = "aaab", c = "b"
Output: [3,2,1,0]


&nbsp;
Constraints:


	1 &lt;= s.length &lt;= 104
	s[i] and c are lowercase English letters.
	It is guaranteed that c occurs at least once in s.


class Solution:
    def shortestToChar(self, s: str, c: str) -> List[int]:
        res = []
        ch= []
        for i in range(len(s)):
            if s[i] == c:
                ch.append(i)
        min_d = len(s)
        for i in range(len(s)):
            for j in range(len(ch)):
                min_d = min(min_d, abs(i-ch[j]))
            res.append(min_d)
            min_d = len(s)
        return res
class Solution {
    public int[] shortestToChar(String s, char c) {
        int n = s.length();
        int index = -1;
        int[] ans = new int[n];
        // Starting from index 0 and storing the distance from the next c;
        for(int i=0;i<n;i++){
            if(s.charAt(i)==c) index = i;//to store the index of the nearest previous c

            if(index==-1) ans[i] = Integer.MAX_VALUE;
            else ans[i] = i-index;
        }

        // Starting from thr end and storing the distance from the previous c;
        index = -1;
        for(int i=n-1;i>=0;i--){
            if(s.charAt(i)==c) index = i;//to store the index of the nearest next c

            if(index!=-1) ans[i] = Math.min(ans[i],index-i);
        }
        return ans;
    }
}
class Solution {
public:
    vector<int> shortestToChar(string s, char c) {
        vector<int>sk;
        vector<int>res;
        vector<int>temp;
        int ss=0;
        int mins;
        for(int i=0;i<s.length();i++){
            if(s[i]==c)
            //storing all the c character location i.e 'e' in sk i.e [3,5,6,11]
             sk.push_back(i);
        }
        for(int i=0;i<s.length();i++){
            for(int j=0;j<sk.size();j++){
            //now subtracting every i value with all value of character locations and storing the minimum in res vector.

                mins=abs(sk[j]-i);
                temp.push_back(mins);
            }
                //in first iteration [3-0=3,5-0=5,6-0=6,11-0=0] so removing the minimum elemnet from this that is 3 and stroing is res.
            int min=temp[0];
            for(auto i:temp){
                if(i<min){
                    min=i;
                }
            }
            res.push_back(min);
            temp.clear();

        }

        return res;
    }
};
/**
 * @param {string} s
 * @param {character} c
 * @return {number[]}
 */
var shortestToChar = function(s, c) {
    
    let result = []
    
    for(let i = 0; i< s.length ; i++) {
        
        if(s.charAt(i) === c) result.push(0)
        else {
            const next = s.indexOf(c,i) === -1 ? Infinity : s.indexOf(c,i) -i
            const prev = result.lastIndexOf(0) === -1 ? Infinity : i- result.lastIndexOf(0) 
            result.push(Math.min(next,prev))
        }
        
    }
    
    return result
};
Shortest Distance to a Character