LC 821. 字符的最短距离

题目描述

这是 LeetCode 上的 821. 字符的最短距离 ,难度为 简单

给你一个字符串 s 和一个字符 c ,且 cs 中出现过的字符。

返回一个整数数组 answer,其中 $answer.length = s.length$ 且 $answer[i]$ 是 s 中从下标 $i$ 到离它最近的字符 c 的距离。

两个下标 $i$ 和 $j$ 之间的距离为 abs(i - j) ,其中 abs 是绝对值函数。

示例 1:

1
2
3
4
5
6
7
8
9
输入:s = "loveleetcode", c = "e"

输出:[3,2,1,0,1,0,0,1,2,2,1,0]

解释:字符 'e' 出现在下标 3、5、6 和 11 处(下标从 0 开始计数)。
距下标 0 最近的 'e' 出现在下标 3 ,所以距离为 abs(0 - 3) = 3
距下标 1 最近的 'e' 出现在下标 3 ,所以距离为 abs(1 - 3) = 2
对于下标 4 ,出现在下标 3 和下标 5 处的 'e' 都离它最近,但距离是一样的 abs(4 - 3) == abs(4 - 5) = 1
距下标 8 最近的 'e' 出现在下标 6 ,所以距离为 abs(8 - 6) = 2

示例 2:
1
2
3
输入:s = "aaab", c = "b"

输出:[3,2,1,0]

提示:

  • $1 <= s.length <= 10^4$
  • $s[i]$ 和 c 均为小写英文字母
  • 题目数据保证 cs 中至少出现一次

遍历

根据题意进行模拟即可。

两次遍历,第一次找到每个 $i$ 左边最近的 c,第二次找到每个 $i$ 右边最近的 c

Java 代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public int[] shortestToChar(String s, char c) {
int n = s.length();
int[] ans = new int[n];
Arrays.fill(ans, n + 1);
for (int i = 0, j = -1; i < n; i++) {
if (s.charAt(i) == c) j = i;
if (j != -1) ans[i] = i - j;
}
for (int i = n - 1, j = -1; i >= 0; i--) {
if (s.charAt(i) == c) j = i;
if (j != -1) ans[i] = Math.min(ans[i], j - i);
}
return ans;
}
}

C++ 代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
vector<int> shortestToChar(string s, char c) {
int n = s.length();
vector<int> ans(n, n + 1);
for (int i = 0, j = -1; i < n; i++) {
if (s[i] == c) j = i;
if (j != -1) ans[i] = i - j;
}
for (int i = n - 1, j = -1; i >= 0; i--) {
if (s[i] == c) j = i;
if (j != -1) ans[i] = min(ans[i], j - i);
}
return ans;
}
};

Python 代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution:
def shortestToChar(self, s: str, c: str) -> List[int]:
n = len(s)
ans = [n + 1] * n
j = -1
for i in range(n):
if s[i] == c:
j = i
if j != - 1:
ans[i] = i - j
j = -1
for i in range(n - 1, -1, -1):
if s[i] == c:
j = i
if j != - 1:
ans[i] = min(ans[i], j - i)
return ans

TypeScript 代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
function shortestToChar(s: string, c: string): number[] {
const n = s.length;
const ans = new Array(n).fill(n + 1);
for (let i = 0, j = -1; i < n; i++) {
if (s.charAt(i) === c) j = i;
if (j !== -1) ans[i] = i - j;
}
for (let i = n - 1, j = -1; i >= 0; i--) {
if (s.charAt(i) === c) j = i;
if (j !== -1) ans[i] = Math.min(ans[i], j - i);
}
return ans;
};

  • 时间复杂度:$O(n)$
  • 空间复杂度:$O(1)$

BFS

起始令所有的 $ans[i] = -1$,然后将所有的 c 字符的下标入队,并更新 $ans[i] = 0$,然后跑一遍 BFS 逻辑,通过 $ans[i]$ 是否为 $-1$ 来判断是否重复入队。

Java 代码:

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
class Solution {
public int[] shortestToChar(String s, char c) {
int n = s.length();
int[] ans = new int[n];
Arrays.fill(ans, -1);
Deque<Integer> d = new ArrayDeque<>();
for (int i = 0; i < n; i++) {
if (s.charAt(i) == c) {
d.addLast(i);
ans[i] = 0;
}
}
int[] dirs = new int[]{-1, 1};
while (!d.isEmpty()) {
int t = d.pollFirst();
for (int di : dirs) {
int ne = t + di;
if (ne >= 0 && ne < n && ans[ne] == -1) {
ans[ne] = ans[t] + 1;
d.addLast(ne);
}
}
}
return ans;
}
}

C++ 代码:
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
class Solution {
public:
vector<int> shortestToChar(string s, char c) {
int n = s.length();
vector<int> ans(n, -1);
deque<int> d;
for (int i = 0; i < n; i++) {
if (s[i] == c) {
d.push_back(i);
ans[i] = 0;
}
}
vector<int> dirs = {-1, 1};
while (!d.empty()) {
int t = d.front();
d.pop_front();
for (auto di : dirs) {
int ne = t + di;
if (ne >= 0 && ne < n && ans[ne] == -1) {
ans[ne] = ans[t] + 1;
d.push_back(ne);
}
}
}
return ans;
}
};

Python 代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution:
def shortestToChar(self, s: str, c: str) -> List[int]:
n = len(s)
ans = [-1] * n
d = deque()
for i in range(n):
if s[i] == c:
d.append(i)
ans[i] = 0
dirs = [-1, 1]
while d:
t = d.popleft()
for di in dirs:
ne = t + di
if 0 <= ne < n and ans[ne] == -1:
ans[ne] = ans[t] + 1
d.append(ne)
return ans

TypeScript 代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function shortestToChar(s: string, c: string): number[] {
const n = s.length;
const ans = new Array(n).fill(-1);
const d = [];
for (let i = 0; i < n; i++) {
if (s.charAt(i) === c) {
d.push(i);
ans[i] = 0;
}
}
const dirs = [-1, 1];
while (d.length > 0) {
const t = d.shift() as number;
for (const di of dirs) {
const ne = t + di;
if (ne >= 0 && ne < n && ans[ne] === -1) {
ans[ne] = ans[t] + 1;
d.push(ne);
}
}
}
return ans;
};

  • 时间复杂度:$O(n)$
  • 空间复杂度:$O(n)$

最后

这是我们「刷穿 LeetCode」系列文章的第 No.821 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。

在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。

为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode

在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!