-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString821.java
More file actions
33 lines (31 loc) · 787 Bytes
/
String821.java
File metadata and controls
33 lines (31 loc) · 787 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
package string;
import java.util.HashSet;
import java.util.Set;
/**
* @ProjectName: leetcode
* @Package: string
* @ClassName: String821
* @Author: markey
* @Description:
* @Date: 2020/5/22 22:53
* @Version: 1.0
*/
public class String821 {
public int[] shortestToChar(String S, char C) {
Set<Integer> location = new HashSet<>();
for (int i = 0; i < S.length(); i++) {
if (S.charAt(i) == C) {
location.add(i);
}
}
int[] res = new int[S.length()];
for (int i = 0; i < S.length(); i++) {
int min = Integer.MAX_VALUE;
for(int j : location) {
min = Math.min(min, Math.abs(j - i));
}
res[i] = min;
}
return res;
}
}