-
Notifications
You must be signed in to change notification settings - Fork 564
Expand file tree
/
Copy path07. Shortest Distance to a Character.cpp
More file actions
52 lines (44 loc) · 1.03 KB
/
07. Shortest Distance to a Character.cpp
File metadata and controls
52 lines (44 loc) · 1.03 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
Shortest Distance to a Character
================================
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]
Example 2:
Input: s = "aaab", c = "b"
Output: [3,2,1,0]
Constraints:
1 <= s.length <= 104
s[i] and c are lowercase English letters.
c occurs at least once in s.
*/
class Solution
{
public:
vector<int> shortestToChar(string s, char c)
{
int n = s.size();
vector<int> left(n), right(n);
vector<int> ans(n);
int curr = INT_MAX;
for (int i = 0; i < n; ++i)
{
if (s[i] == c)
curr = i;
left[i] = curr;
}
curr = INT_MAX;
for (int i = n - 1; i >= 0; --i)
{
if (s[i] == c)
curr = i;
right[i] = curr;
}
for (int i = 0; i < n; ++i)
{
ans[i] = min(abs(i - left[i]), abs(i - right[i]));
}
return ans;
}
};