1
class Solution {
2
public:
3
vector<int> shortestToChar(string s, char c) {
4
vector<int> sk;
5
vector<int> res;
6
vector<int> temp;
7
int ss = 0;
8
int mins;
9
for (int i = 0; i < s.length(); i++) {
10
if (s[i] == c)
11
// storing all the c character location i.e 'e' in sk i.e [3,5,6,11]
12
sk.push_back(i);
13
}
14
for (int i = 0; i < s.length(); i++) {
15
for (int j = 0; j < sk.size(); j++) {
16
// now subtracting every i value with all value of character locations
17
// and storing the minimum in res vector.
18

19
mins = abs(sk[j] - i);
20
temp.push_back(mins);
21
}
22
// in first iteration [3-0=3,5-0=5,6-0=6,11-0=0] so removing the minimum
23
// elemnet from this that is 3 and stroing is res.
24
int min = temp[0];
25
for (auto i : temp) {
26
if (i < min) {
27
min = i;
28
}
29
}
30
res.push_back(min);
31
temp.clear();
32
}
33

34
return res;
35
}
36
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0