forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShortest Distance to a Character.cpp
36 lines (33 loc) · 1.07 KB
/
Shortest Distance to a Character.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Runtime: 11 ms (Top 11.73%) | Memory: 6.7 MB (Top 59.28%)
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;
}
};