Skip to content

Commit 0bdd794

Browse files
committed
feat: add solutions to lc problem: No.0821
No.0821.Shortest Distance to a Character
1 parent 7e62be4 commit 0bdd794

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

solution/0800-0899/0821.Shortest Distance to a Character/README.md

+51
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,57 @@ function shortestToChar(s: string, c: string): number[] {
8484
}
8585
```
8686

87+
```ts
88+
function shortestToChar(s: string, c: string): number[] {
89+
const n = s.length;
90+
const idxs = [];
91+
for (let i = 0; i < n; i++) {
92+
if (s[i] === c) {
93+
idxs.push(i);
94+
}
95+
}
96+
idxs.push(Infinity);
97+
98+
const res = new Array(n);
99+
let i = 0;
100+
for (let j = 0; j < n; j++) {
101+
if (Math.abs(idxs[i] - j) > Math.abs(idxs[i + 1] - j)) {
102+
i++;
103+
}
104+
res[j] = Math.abs(idxs[i] - j);
105+
}
106+
return res;
107+
}
108+
```
109+
110+
### **Rust**
111+
112+
```rust
113+
impl Solution {
114+
pub fn shortest_to_char(s: String, c: char) -> Vec<i32> {
115+
let c = c as u8;
116+
let s = s.as_bytes();
117+
let n = s.len();
118+
let mut res = vec![i32::MAX; n];
119+
let mut pre = i32::MAX;
120+
for i in 0..n {
121+
if s[i] == c {
122+
pre = i as i32;
123+
}
124+
res[i] = i32::abs(i as i32 - pre);
125+
}
126+
pre = i32::MAX;
127+
for i in (0..n).rev() {
128+
if s[i] == c {
129+
pre = i as i32;
130+
}
131+
res[i] = res[i].min(i32::abs(i as i32 - pre));
132+
}
133+
res
134+
}
135+
}
136+
```
137+
87138
### **...**
88139

89140
```

solution/0800-0899/0821.Shortest Distance to a Character/README_EN.md

+51
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,57 @@ function shortestToChar(s: string, c: string): number[] {
7373
}
7474
```
7575

76+
```ts
77+
function shortestToChar(s: string, c: string): number[] {
78+
const n = s.length;
79+
const idxs = [];
80+
for (let i = 0; i < n; i++) {
81+
if (s[i] === c) {
82+
idxs.push(i);
83+
}
84+
}
85+
idxs.push(Infinity);
86+
87+
const res = new Array(n);
88+
let i = 0;
89+
for (let j = 0; j < n; j++) {
90+
if (Math.abs(idxs[i] - j) > Math.abs(idxs[i + 1] - j)) {
91+
i++;
92+
}
93+
res[j] = Math.abs(idxs[i] - j);
94+
}
95+
return res;
96+
}
97+
```
98+
99+
### **Rust**
100+
101+
```rust
102+
impl Solution {
103+
pub fn shortest_to_char(s: String, c: char) -> Vec<i32> {
104+
let c = c as u8;
105+
let s = s.as_bytes();
106+
let n = s.len();
107+
let mut res = vec![i32::MAX; n];
108+
let mut pre = i32::MAX;
109+
for i in 0..n {
110+
if s[i] == c {
111+
pre = i as i32;
112+
}
113+
res[i] = i32::abs(i as i32 - pre);
114+
}
115+
pre = i32::MAX;
116+
for i in (0..n).rev() {
117+
if s[i] == c {
118+
pre = i as i32;
119+
}
120+
res[i] = res[i].min(i32::abs(i as i32 - pre));
121+
}
122+
res
123+
}
124+
}
125+
```
126+
76127
### **...**
77128

78129
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
impl Solution {
2+
pub fn shortest_to_char(s: String, c: char) -> Vec<i32> {
3+
let c = c as u8;
4+
let s = s.as_bytes();
5+
let n = s.len();
6+
let mut res = vec![i32::MAX; n];
7+
let mut pre = i32::MAX;
8+
for i in 0..n {
9+
if s[i] == c {
10+
pre = i as i32;
11+
}
12+
res[i] = i32::abs(i as i32 - pre);
13+
}
14+
pre = i32::MAX;
15+
for i in (0..n).rev() {
16+
if s[i] == c {
17+
pre = i as i32;
18+
}
19+
res[i] = res[i].min(i32::abs(i as i32 - pre));
20+
}
21+
res
22+
}
23+
}

0 commit comments

Comments
 (0)