Skip to content

Commit 0fbf16f

Browse files
committed
feat: add solutions to lc problem: No.0942
No.0942.DI String Match
1 parent b70cf40 commit 0fbf16f

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

solution/0900-0999/0942.DI String Match/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,50 @@ func diStringMatch(s string) []int {
141141
}
142142
```
143143

144+
### **TypeScript**
145+
146+
```ts
147+
function diStringMatch(s: string): number[] {
148+
const n = s.length;
149+
const res = new Array(n + 1);
150+
let low = 0;
151+
let high = n;
152+
for (let i = 0; i < n; i++) {
153+
if (s[i] === 'I') {
154+
res[i] = low++;
155+
} else {
156+
res[i] = high--;
157+
}
158+
}
159+
res[n] = low;
160+
return res;
161+
}
162+
```
163+
164+
### **Rust**
165+
166+
```rust
167+
impl Solution {
168+
pub fn di_string_match(s: String) -> Vec<i32> {
169+
let s = s.as_bytes();
170+
let n = s.len();
171+
let mut res = Vec::with_capacity(n + 1);
172+
let (mut low, mut high) = (-1, (n + 1) as i32);
173+
for i in 0..n {
174+
res.push(if s[i] == b'I' {
175+
low += 1;
176+
low
177+
} else {
178+
high -= 1;
179+
high
180+
});
181+
}
182+
res.push(low + 1);
183+
res
184+
}
185+
}
186+
```
187+
144188
### **...**
145189

146190
```

solution/0900-0999/0942.DI String Match/README_EN.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,50 @@ func diStringMatch(s string) []int {
119119
}
120120
```
121121

122+
### **TypeScript**
123+
124+
```ts
125+
function diStringMatch(s: string): number[] {
126+
const n = s.length;
127+
const res = new Array(n + 1);
128+
let low = 0;
129+
let high = n;
130+
for (let i = 0; i < n; i++) {
131+
if (s[i] === 'I') {
132+
res[i] = low++;
133+
} else {
134+
res[i] = high--;
135+
}
136+
}
137+
res[n] = low;
138+
return res;
139+
}
140+
```
141+
142+
### **Rust**
143+
144+
```rust
145+
impl Solution {
146+
pub fn di_string_match(s: String) -> Vec<i32> {
147+
let s = s.as_bytes();
148+
let n = s.len();
149+
let mut res = Vec::with_capacity(n + 1);
150+
let (mut low, mut high) = (-1, (n + 1) as i32);
151+
for i in 0..n {
152+
res.push(if s[i] == b'I' {
153+
low += 1;
154+
low
155+
} else {
156+
high -= 1;
157+
high
158+
});
159+
}
160+
res.push(low + 1);
161+
res
162+
}
163+
}
164+
```
165+
122166
### **...**
123167

124168
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
impl Solution {
2+
pub fn di_string_match(s: String) -> Vec<i32> {
3+
let s = s.as_bytes();
4+
let n = s.len();
5+
let mut res = Vec::with_capacity(n + 1);
6+
let (mut low, mut high) = (-1, (n + 1) as i32);
7+
for i in 0..n {
8+
res.push(if s[i] == b'I' {
9+
low += 1;
10+
low
11+
} else {
12+
high -= 1;
13+
high
14+
});
15+
}
16+
res.push(low + 1);
17+
res
18+
}
19+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function diStringMatch(s: string): number[] {
2+
const n = s.length;
3+
const res = new Array(n + 1);
4+
let low = 0;
5+
let high = n;
6+
for (let i = 0; i < n; i++) {
7+
if (s[i] === 'I') {
8+
res[i] = low++;
9+
} else {
10+
res[i] = high--;
11+
}
12+
}
13+
res[n] = low;
14+
return res;
15+
}

0 commit comments

Comments
 (0)