-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day_7.cpp
39 lines (37 loc) · 1 KB
/
Day_7.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
37
38
39
/*
Question:
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 shortest distance from s[i] to the character c in s.
Example 1:
Input: s = "loveleetcode", c = "e"
Output: [3,2,1,0,1,0,0,1,2,2,1,0]
*/
class Solution {
public:
vector<int> shortestToChar(string s, char c) //Solved by Brute Force
{
vector<int>v,v1;
for(int i=0;i<s.size();i++) //For storing indexes of c in string s
{
if(s[i]==c)
v1.push_back(i);
}
int mi=INT_MAX;
for(int i=0;i<s.size();i++)
{
if(s[i]==c)
v.push_back(0);
else
{
for(auto u:v1)
{
if(abs(u-i) < mi)
mi=abs(u-i);
}
v.push_back(mi);
mi=INT_MAX;
}
}
return v;
}
};