-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortestPath.cpp
More file actions
36 lines (35 loc) · 915 Bytes
/
ShortestPath.cpp
File metadata and controls
36 lines (35 loc) · 915 Bytes
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
class Solution {
public:
vector<int> shortestToChar(string S, char C) {
vector<int> dest;
for(int i =0;i<S.size();i++){
if(S[i]==C)
dest.push_back(i);
}
int prev = -1;
int index= 0 ;
int next= dest[index];
vector<int> output;
for(int i=0;i<S.size();i++){
if(i <= next){
if(prev==-1)
output.push_back(next-i);
else
output.push_back(min(i-prev,next-i));
}
else{
prev=next;
index++;
if(index>=dest.size()){
next = -1;
output.push_back(i-dest[dest.size()-1]);
}else
{
next = dest[index];
output.push_back(min(i-prev,next-i));
}
}
}
return output;
}
};