Skip to content

Commit bbb519f

Browse files
authored
feat: update solutions to lc problem: No.0942 (#3210)
No.0942.DI String Match
1 parent 711ad71 commit bbb519f

File tree

6 files changed

+98
-98
lines changed

6 files changed

+98
-98
lines changed

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

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,13 @@ tags:
6565

6666
<!-- solution:start -->
6767

68-
### 方法一
68+
### 方法一:贪心
69+
70+
我们可以使用两个指针 `low``high` 分别表示当前的最小值和最大值,然后遍历字符串 `s`,如果当前字符是 `I`,那么我们就将 `low` 加入到结果数组中,并且 `low` 自增 1;如果当前字符是 `D`,那么我们就将 `high` 加入到结果数组中,并且 `high` 自减 1。
71+
72+
最后,我们将 `low` 加入到结果数组中,返回结果数组即可。
73+
74+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 `s` 的长度。
6975

7076
<!-- tabs:start -->
7177

@@ -74,11 +80,10 @@ tags:
7480
```python
7581
class Solution:
7682
def diStringMatch(self, s: str) -> List[int]:
77-
n = len(s)
78-
low, high = 0, n
83+
low, high = 0, len(s)
7984
ans = []
80-
for i in range(n):
81-
if s[i] == 'I':
85+
for c in s:
86+
if c == "I":
8287
ans.append(low)
8388
low += 1
8489
else:
@@ -134,12 +139,10 @@ public:
134139
#### Go
135140
136141
```go
137-
func diStringMatch(s string) []int {
138-
n := len(s)
139-
low, high := 0, n
140-
var ans []int
141-
for i := 0; i < n; i++ {
142-
if s[i] == 'I' {
142+
func diStringMatch(s string) (ans []int) {
143+
low, high := 0, len(s)
144+
for _, c := range s {
145+
if c == 'I' {
143146
ans = append(ans, low)
144147
low++
145148
} else {
@@ -148,27 +151,25 @@ func diStringMatch(s string) []int {
148151
}
149152
}
150153
ans = append(ans, low)
151-
return ans
154+
return
152155
}
153156
```
154157

155158
#### TypeScript
156159

157160
```ts
158161
function diStringMatch(s: string): number[] {
159-
const n = s.length;
160-
const res = new Array(n + 1);
161-
let low = 0;
162-
let high = n;
163-
for (let i = 0; i < n; i++) {
164-
if (s[i] === 'I') {
165-
res[i] = low++;
162+
const ans: number[] = [];
163+
let [low, high] = [0, s.length];
164+
for (const c of s) {
165+
if (c === 'I') {
166+
ans.push(low++);
166167
} else {
167-
res[i] = high--;
168+
ans.push(high--);
168169
}
169170
}
170-
res[n] = low;
171-
return res;
171+
ans.push(low);
172+
return ans;
172173
}
173174
```
174175

@@ -177,21 +178,22 @@ function diStringMatch(s: string): number[] {
177178
```rust
178179
impl Solution {
179180
pub fn di_string_match(s: String) -> Vec<i32> {
180-
let s = s.as_bytes();
181-
let n = s.len();
182-
let mut res = Vec::with_capacity(n + 1);
183-
let (mut low, mut high) = (-1, (n + 1) as i32);
184-
for i in 0..n {
185-
res.push(if s[i] == b'I' {
181+
let mut low = 0;
182+
let mut high = s.len() as i32;
183+
let mut ans = Vec::with_capacity(s.len() + 1);
184+
185+
for c in s.chars() {
186+
if c == 'I' {
187+
ans.push(low);
186188
low += 1;
187-
low
188189
} else {
190+
ans.push(high);
189191
high -= 1;
190-
high
191-
});
192+
}
192193
}
193-
res.push(low + 1);
194-
res
194+
195+
ans.push(low);
196+
ans
195197
}
196198
}
197199
```

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

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,13 @@ tags:
5353

5454
<!-- solution:start -->
5555

56-
### Solution 1
56+
### Solution 1: Greedy Algorithm
57+
58+
We can use two pointers `low` and `high` to represent the current minimum and maximum values, respectively. Then, we traverse the string `s`. If the current character is `I`, we add `low` to the result array, and increment `low` by 1; if the current character is `D`, we add `high` to the result array, and decrement `high` by 1.
59+
60+
Finally, we add `low` to the result array and return the result array.
61+
62+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the string `s`.
5763

5864
<!-- tabs:start -->
5965

@@ -62,11 +68,10 @@ tags:
6268
```python
6369
class Solution:
6470
def diStringMatch(self, s: str) -> List[int]:
65-
n = len(s)
66-
low, high = 0, n
71+
low, high = 0, len(s)
6772
ans = []
68-
for i in range(n):
69-
if s[i] == 'I':
73+
for c in s:
74+
if c == "I":
7075
ans.append(low)
7176
low += 1
7277
else:
@@ -122,12 +127,10 @@ public:
122127
#### Go
123128
124129
```go
125-
func diStringMatch(s string) []int {
126-
n := len(s)
127-
low, high := 0, n
128-
var ans []int
129-
for i := 0; i < n; i++ {
130-
if s[i] == 'I' {
130+
func diStringMatch(s string) (ans []int) {
131+
low, high := 0, len(s)
132+
for _, c := range s {
133+
if c == 'I' {
131134
ans = append(ans, low)
132135
low++
133136
} else {
@@ -136,27 +139,25 @@ func diStringMatch(s string) []int {
136139
}
137140
}
138141
ans = append(ans, low)
139-
return ans
142+
return
140143
}
141144
```
142145

143146
#### TypeScript
144147

145148
```ts
146149
function diStringMatch(s: string): number[] {
147-
const n = s.length;
148-
const res = new Array(n + 1);
149-
let low = 0;
150-
let high = n;
151-
for (let i = 0; i < n; i++) {
152-
if (s[i] === 'I') {
153-
res[i] = low++;
150+
const ans: number[] = [];
151+
let [low, high] = [0, s.length];
152+
for (const c of s) {
153+
if (c === 'I') {
154+
ans.push(low++);
154155
} else {
155-
res[i] = high--;
156+
ans.push(high--);
156157
}
157158
}
158-
res[n] = low;
159-
return res;
159+
ans.push(low);
160+
return ans;
160161
}
161162
```
162163

@@ -165,21 +166,22 @@ function diStringMatch(s: string): number[] {
165166
```rust
166167
impl Solution {
167168
pub fn di_string_match(s: String) -> Vec<i32> {
168-
let s = s.as_bytes();
169-
let n = s.len();
170-
let mut res = Vec::with_capacity(n + 1);
171-
let (mut low, mut high) = (-1, (n + 1) as i32);
172-
for i in 0..n {
173-
res.push(if s[i] == b'I' {
169+
let mut low = 0;
170+
let mut high = s.len() as i32;
171+
let mut ans = Vec::with_capacity(s.len() + 1);
172+
173+
for c in s.chars() {
174+
if c == 'I' {
175+
ans.push(low);
174176
low += 1;
175-
low
176177
} else {
178+
ans.push(high);
177179
high -= 1;
178-
high
179-
});
180+
}
180181
}
181-
res.push(low + 1);
182-
res
182+
183+
ans.push(low);
184+
ans
183185
}
184186
}
185187
```
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
func diStringMatch(s string) []int {
2-
n := len(s)
3-
low, high := 0, n
4-
var ans []int
5-
for i := 0; i < n; i++ {
6-
if s[i] == 'I' {
1+
func diStringMatch(s string) (ans []int) {
2+
low, high := 0, len(s)
3+
for _, c := range s {
4+
if c == 'I' {
75
ans = append(ans, low)
86
low++
97
} else {
@@ -12,5 +10,5 @@ func diStringMatch(s string) []int {
1210
}
1311
}
1412
ans = append(ans, low)
15-
return ans
13+
return
1614
}

solution/0900-0999/0942.DI String Match/Solution.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
class Solution:
22
def diStringMatch(self, s: str) -> List[int]:
3-
n = len(s)
4-
low, high = 0, n
3+
low, high = 0, len(s)
54
ans = []
6-
for i in range(n):
7-
if s[i] == 'I':
5+
for c in s:
6+
if c == "I":
87
ans.append(low)
98
low += 1
109
else:
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
impl Solution {
22
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' {
3+
let mut low = 0;
4+
let mut high = s.len() as i32;
5+
let mut ans = Vec::with_capacity(s.len() + 1);
6+
7+
for c in s.chars() {
8+
if c == 'I' {
9+
ans.push(low);
910
low += 1;
10-
low
1111
} else {
12+
ans.push(high);
1213
high -= 1;
13-
high
14-
});
14+
}
1515
}
16-
res.push(low + 1);
17-
res
16+
17+
ans.push(low);
18+
ans
1819
}
1920
}
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
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++;
2+
const ans: number[] = [];
3+
let [low, high] = [0, s.length];
4+
for (const c of s) {
5+
if (c === 'I') {
6+
ans.push(low++);
97
} else {
10-
res[i] = high--;
8+
ans.push(high--);
119
}
1210
}
13-
res[n] = low;
14-
return res;
11+
ans.push(low);
12+
return ans;
1513
}

0 commit comments

Comments
 (0)