Skip to content

Commit e265658

Browse files
authored
Update README_EN.md
1 parent 6494287 commit e265658

File tree

1 file changed

+139
-145
lines changed
  • solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses

1 file changed

+139
-145
lines changed

solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README_EN.md

Lines changed: 139 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ tags:
6666

6767
### Solution 1: Simulation
6868

69-
We can use a double-ended queue or stack to simulate the reversal process.
69+
We can directly use a stack to simulate the reversal process.
7070

71-
The time complexity is $O(n^2)$, where $n$ is the length of the string $s$.
71+
The time complexity is $O(n^2)$, and the space complexity is $O(n)$, where $n$ is the length of the string $s$.
7272

7373
<!-- tabs:start -->
7474

@@ -79,46 +79,37 @@ class Solution:
7979
def reverseParentheses(self, s: str) -> str:
8080
stk = []
8181
for c in s:
82-
if c == ')':
82+
if c == ")":
8383
t = []
84-
while stk[-1] != '(':
84+
while stk[-1] != "(":
8585
t.append(stk.pop())
8686
stk.pop()
8787
stk.extend(t)
8888
else:
8989
stk.append(c)
90-
return ''.join(stk)
90+
return "".join(stk)
9191
```
9292

9393
#### Java
9494

9595
```java
9696
class Solution {
9797
public String reverseParentheses(String s) {
98-
int n = s.length();
99-
int[] d = new int[n];
100-
Deque<Integer> stk = new ArrayDeque<>();
101-
for (int i = 0; i < n; ++i) {
102-
if (s.charAt(i) == '(') {
103-
stk.push(i);
104-
} else if (s.charAt(i) == ')') {
105-
int j = stk.pop();
106-
d[i] = j;
107-
d[j] = i;
108-
}
109-
}
110-
StringBuilder ans = new StringBuilder();
111-
int i = 0, x = 1;
112-
while (i < n) {
113-
if (s.charAt(i) == '(' || s.charAt(i) == ')') {
114-
i = d[i];
115-
x = -x;
98+
StringBuilder stk = new StringBuilder();
99+
for (char c : s.toCharArray()) {
100+
if (c == ')') {
101+
StringBuilder t = new StringBuilder();
102+
while (stk.charAt(stk.length() - 1) != '(') {
103+
t.append(stk.charAt(stk.length() - 1));
104+
stk.deleteCharAt(stk.length() - 1);
105+
}
106+
stk.deleteCharAt(stk.length() - 1);
107+
stk.append(t);
116108
} else {
117-
ans.append(s.charAt(i));
109+
stk.append(c);
118110
}
119-
i += x;
120111
}
121-
return ans.toString();
112+
return stk.toString();
122113
}
123114
}
124115
```
@@ -170,6 +161,27 @@ func reverseParentheses(s string) string {
170161
}
171162
```
172163

164+
#### TypeScript
165+
166+
```ts
167+
function reverseParentheses(s: string): string {
168+
const stk: string[] = [];
169+
for (const c of s) {
170+
if (c === ')') {
171+
const t: string[] = [];
172+
while (stk.at(-1)! !== '(') {
173+
t.push(stk.pop()!);
174+
}
175+
stk.pop();
176+
stk.push(...t);
177+
} else {
178+
stk.push(c);
179+
}
180+
}
181+
return stk.join('');
182+
}
183+
```
184+
173185
#### JavaScript
174186

175187
```js
@@ -178,83 +190,38 @@ func reverseParentheses(s string) string {
178190
* @return {string}
179191
*/
180192
var reverseParentheses = function (s) {
181-
const n = s.length;
182-
const d = new Array(n).fill(0);
183193
const stk = [];
184-
for (let i = 0; i < n; ++i) {
185-
if (s[i] == '(') {
186-
stk.push(i);
187-
} else if (s[i] == ')') {
188-
const j = stk.pop();
189-
d[i] = j;
190-
d[j] = i;
191-
}
192-
}
193-
let i = 0;
194-
let x = 1;
195-
const ans = [];
196-
while (i < n) {
197-
const c = s.charAt(i);
198-
if (c == '(' || c == ')') {
199-
i = d[i];
200-
x = -x;
194+
for (const c of s) {
195+
if (c === ')') {
196+
const t = [];
197+
while (stk.at(-1) !== '(') {
198+
t.push(stk.pop());
199+
}
200+
stk.pop();
201+
stk.push(...t);
201202
} else {
202-
ans.push(c);
203+
stk.push(c);
203204
}
204-
i += x;
205205
}
206-
return ans.join('');
206+
return stk.join('');
207207
};
208208
```
209209

210-
#### TypeScript
211-
212-
```ts
213-
function reverseParentheses(s: string): string {
214-
const n = s.length;
215-
const d = new Array(n).fill(0);
216-
const stk: number[] = [];
217-
for (let i = 0; i < n; ++i) {
218-
if (s[i] === '(') {
219-
stk.push(i);
220-
} else if (s[i] === ')') {
221-
const j = stk.pop()!;
222-
d[i] = j;
223-
d[j] = i;
224-
}
225-
}
226-
let i = 0;
227-
let x = 1;
228-
const ans: string[] = [];
229-
while (i < n) {
230-
const c = s.charAt(i);
231-
if (c === '(' || c === ')') {
232-
i = d[i];
233-
x = -x;
234-
} else {
235-
ans.push(c);
236-
}
237-
i += x;
238-
}
239-
return ans.join('');
240-
}
241-
```
242-
243210
<!-- tabs:end -->
244211

245212
<!-- solution:end -->
246213

247214
<!-- solution:start -->
248215

249-
### Solution 2: Quick Thinking
216+
### Solution 2: Brain Teaser
250217

251-
We observe that during the traversal of the string, each time we encounter '(' or ')', we jump to the corresponding ')' or '(', then reverse the traversal direction and continue.
218+
We observe that, when traversing the string, each time we encounter `(` or `)`, we jump to the corresponding `)` or `(` and then reverse the direction of traversal to continue.
252219

253-
Therefore, we can use an array $d$ to record the position of the other bracket corresponding to each '(' or ')', i.e., $d[i]$ represents the position of the other bracket corresponding to the bracket at position $i$. We can directly use a stack to calculate the array $d$.
220+
Therefore, we can use an array $d$ to record the position of the corresponding other bracket for each `(` or `)`, i.e., $d[i]$ represents the position of the other bracket corresponding to the bracket at position $i$. We can directly use a stack to compute the array $d$.
254221

255-
Then, we traverse the string from left to right. When we encounter '(' or ')', we jump to the corresponding position according to the array $d$, then reverse the direction and continue to traverse until the entire string is traversed.
222+
Then, we traverse the string from left to right. When encountering `(` or `)`, we jump to the corresponding position according to the array $d$, then reverse the direction and continue traversing until the entire string is traversed.
256223

257-
The time complexity is $O(n)$, where $n$ is the length of the string $s$.
224+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the string $s$.
258225

259226
<!-- tabs:start -->
260227

@@ -267,21 +234,54 @@ class Solution:
267234
d = [0] * n
268235
stk = []
269236
for i, c in enumerate(s):
270-
if c == '(':
237+
if c == "(":
271238
stk.append(i)
272-
elif c == ')':
239+
elif c == ")":
273240
j = stk.pop()
274241
d[i], d[j] = j, i
275242
i, x = 0, 1
276243
ans = []
277244
while i < n:
278-
if s[i] in '()':
245+
if s[i] in "()":
279246
i = d[i]
280247
x = -x
281248
else:
282249
ans.append(s[i])
283250
i += x
284-
return ''.join(ans)
251+
return "".join(ans)
252+
```
253+
254+
#### Java
255+
256+
```java
257+
class Solution {
258+
public String reverseParentheses(String s) {
259+
int n = s.length();
260+
int[] d = new int[n];
261+
Deque<Integer> stk = new ArrayDeque<>();
262+
for (int i = 0; i < n; ++i) {
263+
if (s.charAt(i) == '(') {
264+
stk.push(i);
265+
} else if (s.charAt(i) == ')') {
266+
int j = stk.pop();
267+
d[i] = j;
268+
d[j] = i;
269+
}
270+
}
271+
StringBuilder ans = new StringBuilder();
272+
int i = 0, x = 1;
273+
while (i < n) {
274+
if (s.charAt(i) == '(' || s.charAt(i) == ')') {
275+
i = d[i];
276+
x = -x;
277+
} else {
278+
ans.append(s.charAt(i));
279+
}
280+
i += x;
281+
}
282+
return ans.toString();
283+
}
284+
}
285285
```
286286

287287
#### C++
@@ -350,80 +350,74 @@ func reverseParentheses(s string) string {
350350
}
351351
```
352352

353-
#### JavaScript
353+
#### TypeScript
354354

355-
```js
356-
function reverseParentheses(s) {
357-
const res = [];
355+
```ts
356+
function reverseParentheses(s: string): string {
358357
const n = s.length;
359-
const d = Array(n).fill(-1);
360-
const stk = [];
361-
362-
for (let i = 0; i < n; i++) {
363-
if (s[i] === '(') stk.push(i);
364-
else if (s[i] === ')') {
365-
const j = stk.pop();
358+
const d: number[] = Array(n).fill(0);
359+
const stk: number[] = [];
360+
for (let i = 0; i < n; ++i) {
361+
if (s[i] === '(') {
362+
stk.push(i);
363+
} else if (s[i] === ')') {
364+
const j = stk.pop()!;
366365
d[i] = j;
367366
d[j] = i;
368367
}
369368
}
370-
371-
for (let i = 0, forward = true; i < n; ) {
372-
const ch = s[i];
373-
374-
switch (s[i]) {
375-
case '(':
376-
case ')':
377-
i = forward ? d[i] - 1 : d[i] + 1;
378-
forward = !forward;
379-
break;
380-
381-
default:
382-
res.push(ch);
383-
i = forward ? i + 1 : i - 1;
369+
let i = 0;
370+
let x = 1;
371+
const ans: string[] = [];
372+
while (i < n) {
373+
const c = s.charAt(i);
374+
if ('()'.includes(c)) {
375+
i = d[i];
376+
x = -x;
377+
} else {
378+
ans.push(c);
384379
}
380+
i += x;
385381
}
386-
387-
return res.join('');
382+
return ans.join('');
388383
}
389384
```
390385

391-
#### TypeScript
386+
#### JavaScript
392387

393-
```ts
394-
function reverseParentheses(s: string): string {
395-
const res: string[] = [];
388+
```js
389+
/**
390+
* @param {string} s
391+
* @return {string}
392+
*/
393+
var reverseParentheses = function (s) {
396394
const n = s.length;
397-
const d = Array(n).fill(-1);
398-
const stk: number[] = [];
399-
400-
for (let i = 0; i < n; i++) {
401-
if (s[i] === '(') stk.push(i);
402-
else if (s[i] === ')') {
403-
const j = stk.pop()!;
395+
const d = Array(n).fill(0);
396+
const stk = [];
397+
for (let i = 0; i < n; ++i) {
398+
if (s[i] === '(') {
399+
stk.push(i);
400+
} else if (s[i] === ')') {
401+
const j = stk.pop();
404402
d[i] = j;
405403
d[j] = i;
406404
}
407405
}
408-
409-
for (let i = 0, forward = true; i < n; ) {
410-
const ch = s[i];
411-
412-
switch (s[i]) {
413-
case '(':
414-
case ')':
415-
i = forward ? d[i] - 1 : d[i] + 1;
416-
forward = !forward;
417-
break;
418-
419-
default:
420-
res.push(ch);
421-
i = forward ? i + 1 : i - 1;
406+
let i = 0;
407+
let x = 1;
408+
const ans = [];
409+
while (i < n) {
410+
const c = s.charAt(i);
411+
if ('()'.includes(c)) {
412+
i = d[i];
413+
x = -x;
414+
} else {
415+
ans.push(c);
422416
}
417+
i += x;
423418
}
424-
425-
return res.join('');
426-
}
419+
return ans.join('');
420+
};
427421
```
428422

429423
<!-- tabs:end -->

0 commit comments

Comments
 (0)