diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README.md b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README.md index fc2e0e6677e9d..7547ec2bfc4dd 100644 --- a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README.md +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README.md @@ -73,9 +73,9 @@ tags: ### 方法一:模拟 -用双端队列或者栈,模拟反转的过程。 +我们可以直接用栈来模拟反转的过程。 -时间复杂度 $O(n^2)$,其中 $n$ 为字符串 $s$ 的长度。 +时间复杂度 $O(n^2)$,空间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。 @@ -86,15 +86,15 @@ class Solution: def reverseParentheses(self, s: str) -> str: stk = [] for c in s: - if c == ')': + if c == ")": t = [] - while stk[-1] != '(': + while stk[-1] != "(": t.append(stk.pop()) stk.pop() stk.extend(t) else: stk.append(c) - return ''.join(stk) + return "".join(stk) ``` #### Java @@ -102,30 +102,21 @@ class Solution: ```java class Solution { public String reverseParentheses(String s) { - int n = s.length(); - int[] d = new int[n]; - Deque stk = new ArrayDeque<>(); - for (int i = 0; i < n; ++i) { - if (s.charAt(i) == '(') { - stk.push(i); - } else if (s.charAt(i) == ')') { - int j = stk.pop(); - d[i] = j; - d[j] = i; - } - } - StringBuilder ans = new StringBuilder(); - int i = 0, x = 1; - while (i < n) { - if (s.charAt(i) == '(' || s.charAt(i) == ')') { - i = d[i]; - x = -x; + StringBuilder stk = new StringBuilder(); + for (char c : s.toCharArray()) { + if (c == ')') { + StringBuilder t = new StringBuilder(); + while (stk.charAt(stk.length() - 1) != '(') { + t.append(stk.charAt(stk.length() - 1)); + stk.deleteCharAt(stk.length() - 1); + } + stk.deleteCharAt(stk.length() - 1); + stk.append(t); } else { - ans.append(s.charAt(i)); + stk.append(c); } - i += x; } - return ans.toString(); + return stk.toString(); } } ``` @@ -177,6 +168,27 @@ func reverseParentheses(s string) string { } ``` +#### TypeScript + +```ts +function reverseParentheses(s: string): string { + const stk: string[] = []; + for (const c of s) { + if (c === ')') { + const t: string[] = []; + while (stk.at(-1)! !== '(') { + t.push(stk.pop()!); + } + stk.pop(); + stk.push(...t); + } else { + stk.push(c); + } + } + return stk.join(''); +} +``` + #### JavaScript ```js @@ -185,32 +197,20 @@ func reverseParentheses(s string) string { * @return {string} */ var reverseParentheses = function (s) { - const n = s.length; - const d = new Array(n).fill(0); const stk = []; - for (let i = 0; i < n; ++i) { - if (s[i] == '(') { - stk.push(i); - } else if (s[i] == ')') { - const j = stk.pop(); - d[i] = j; - d[j] = i; - } - } - let i = 0; - let x = 1; - const ans = []; - while (i < n) { - const c = s.charAt(i); - if (c == '(' || c == ')') { - i = d[i]; - x = -x; + for (const c of s) { + if (c === ')') { + const t = []; + while (stk.at(-1) !== '(') { + t.push(stk.pop()); + } + stk.pop(); + stk.push(...t); } else { - ans.push(c); + stk.push(c); } - i += x; } - return ans.join(''); + return stk.join(''); }; ``` @@ -228,7 +228,7 @@ var reverseParentheses = function (s) { 然后,我们从左到右遍历字符串,遇到 `(` 或者 `)` 时,根据 $d$ 数组跳到对应的位置,然后反转方向,继续遍历,直到遍历完整个字符串。 -时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。 +时间复杂度 $O(n)$,空间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。 @@ -241,21 +241,54 @@ class Solution: d = [0] * n stk = [] for i, c in enumerate(s): - if c == '(': + if c == "(": stk.append(i) - elif c == ')': + elif c == ")": j = stk.pop() d[i], d[j] = j, i i, x = 0, 1 ans = [] while i < n: - if s[i] in '()': + if s[i] in "()": i = d[i] x = -x else: ans.append(s[i]) i += x - return ''.join(ans) + return "".join(ans) +``` + +#### Java + +```java +class Solution { + public String reverseParentheses(String s) { + int n = s.length(); + int[] d = new int[n]; + Deque stk = new ArrayDeque<>(); + for (int i = 0; i < n; ++i) { + if (s.charAt(i) == '(') { + stk.push(i); + } else if (s.charAt(i) == ')') { + int j = stk.pop(); + d[i] = j; + d[j] = i; + } + } + StringBuilder ans = new StringBuilder(); + int i = 0, x = 1; + while (i < n) { + if (s.charAt(i) == '(' || s.charAt(i) == ')') { + i = d[i]; + x = -x; + } else { + ans.append(s.charAt(i)); + } + i += x; + } + return ans.toString(); + } +} ``` #### C++ @@ -324,6 +357,76 @@ func reverseParentheses(s string) string { } ``` +#### TypeScript + +```ts +function reverseParentheses(s: string): string { + const n = s.length; + const d: number[] = Array(n).fill(0); + const stk: number[] = []; + for (let i = 0; i < n; ++i) { + if (s[i] === '(') { + stk.push(i); + } else if (s[i] === ')') { + const j = stk.pop()!; + d[i] = j; + d[j] = i; + } + } + let i = 0; + let x = 1; + const ans: string[] = []; + while (i < n) { + const c = s.charAt(i); + if ('()'.includes(c)) { + i = d[i]; + x = -x; + } else { + ans.push(c); + } + i += x; + } + return ans.join(''); +} +``` + +#### JavaScript + +```js +/** + * @param {string} s + * @return {string} + */ +var reverseParentheses = function (s) { + const n = s.length; + const d = Array(n).fill(0); + const stk = []; + for (let i = 0; i < n; ++i) { + if (s[i] === '(') { + stk.push(i); + } else if (s[i] === ')') { + const j = stk.pop(); + d[i] = j; + d[j] = i; + } + } + let i = 0; + let x = 1; + const ans = []; + while (i < n) { + const c = s.charAt(i); + if ('()'.includes(c)) { + i = d[i]; + x = -x; + } else { + ans.push(c); + } + i += x; + } + return ans.join(''); +}; +``` + diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README_EN.md b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README_EN.md index c0bbc391d7f41..bb301d2ba6bc4 100644 --- a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README_EN.md +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README_EN.md @@ -66,9 +66,9 @@ tags: ### Solution 1: Simulation -We can use a double-ended queue or stack to simulate the reversal process. +We can directly use a stack to simulate the reversal process. -The time complexity is $O(n^2)$, where $n$ is the length of the string $s$. +The time complexity is $O(n^2)$, and the space complexity is $O(n)$, where $n$ is the length of the string $s$. @@ -79,15 +79,15 @@ class Solution: def reverseParentheses(self, s: str) -> str: stk = [] for c in s: - if c == ')': + if c == ")": t = [] - while stk[-1] != '(': + while stk[-1] != "(": t.append(stk.pop()) stk.pop() stk.extend(t) else: stk.append(c) - return ''.join(stk) + return "".join(stk) ``` #### Java @@ -95,30 +95,21 @@ class Solution: ```java class Solution { public String reverseParentheses(String s) { - int n = s.length(); - int[] d = new int[n]; - Deque stk = new ArrayDeque<>(); - for (int i = 0; i < n; ++i) { - if (s.charAt(i) == '(') { - stk.push(i); - } else if (s.charAt(i) == ')') { - int j = stk.pop(); - d[i] = j; - d[j] = i; - } - } - StringBuilder ans = new StringBuilder(); - int i = 0, x = 1; - while (i < n) { - if (s.charAt(i) == '(' || s.charAt(i) == ')') { - i = d[i]; - x = -x; + StringBuilder stk = new StringBuilder(); + for (char c : s.toCharArray()) { + if (c == ')') { + StringBuilder t = new StringBuilder(); + while (stk.charAt(stk.length() - 1) != '(') { + t.append(stk.charAt(stk.length() - 1)); + stk.deleteCharAt(stk.length() - 1); + } + stk.deleteCharAt(stk.length() - 1); + stk.append(t); } else { - ans.append(s.charAt(i)); + stk.append(c); } - i += x; } - return ans.toString(); + return stk.toString(); } } ``` @@ -170,6 +161,27 @@ func reverseParentheses(s string) string { } ``` +#### TypeScript + +```ts +function reverseParentheses(s: string): string { + const stk: string[] = []; + for (const c of s) { + if (c === ')') { + const t: string[] = []; + while (stk.at(-1)! !== '(') { + t.push(stk.pop()!); + } + stk.pop(); + stk.push(...t); + } else { + stk.push(c); + } + } + return stk.join(''); +} +``` + #### JavaScript ```js @@ -178,32 +190,20 @@ func reverseParentheses(s string) string { * @return {string} */ var reverseParentheses = function (s) { - const n = s.length; - const d = new Array(n).fill(0); const stk = []; - for (let i = 0; i < n; ++i) { - if (s[i] == '(') { - stk.push(i); - } else if (s[i] == ')') { - const j = stk.pop(); - d[i] = j; - d[j] = i; - } - } - let i = 0; - let x = 1; - const ans = []; - while (i < n) { - const c = s.charAt(i); - if (c == '(' || c == ')') { - i = d[i]; - x = -x; + for (const c of s) { + if (c === ')') { + const t = []; + while (stk.at(-1) !== '(') { + t.push(stk.pop()); + } + stk.pop(); + stk.push(...t); } else { - ans.push(c); + stk.push(c); } - i += x; } - return ans.join(''); + return stk.join(''); }; ``` @@ -213,15 +213,15 @@ var reverseParentheses = function (s) { -### Solution 2: Quick Thinking +### Solution 2: Brain Teaser -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. +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. -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$. +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$. -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. +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. -The time complexity is $O(n)$, where $n$ is the length of the string $s$. +The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the string $s$. @@ -234,21 +234,54 @@ class Solution: d = [0] * n stk = [] for i, c in enumerate(s): - if c == '(': + if c == "(": stk.append(i) - elif c == ')': + elif c == ")": j = stk.pop() d[i], d[j] = j, i i, x = 0, 1 ans = [] while i < n: - if s[i] in '()': + if s[i] in "()": i = d[i] x = -x else: ans.append(s[i]) i += x - return ''.join(ans) + return "".join(ans) +``` + +#### Java + +```java +class Solution { + public String reverseParentheses(String s) { + int n = s.length(); + int[] d = new int[n]; + Deque stk = new ArrayDeque<>(); + for (int i = 0; i < n; ++i) { + if (s.charAt(i) == '(') { + stk.push(i); + } else if (s.charAt(i) == ')') { + int j = stk.pop(); + d[i] = j; + d[j] = i; + } + } + StringBuilder ans = new StringBuilder(); + int i = 0, x = 1; + while (i < n) { + if (s.charAt(i) == '(' || s.charAt(i) == ')') { + i = d[i]; + x = -x; + } else { + ans.append(s.charAt(i)); + } + i += x; + } + return ans.toString(); + } +} ``` #### C++ @@ -317,6 +350,76 @@ func reverseParentheses(s string) string { } ``` +#### TypeScript + +```ts +function reverseParentheses(s: string): string { + const n = s.length; + const d: number[] = Array(n).fill(0); + const stk: number[] = []; + for (let i = 0; i < n; ++i) { + if (s[i] === '(') { + stk.push(i); + } else if (s[i] === ')') { + const j = stk.pop()!; + d[i] = j; + d[j] = i; + } + } + let i = 0; + let x = 1; + const ans: string[] = []; + while (i < n) { + const c = s.charAt(i); + if ('()'.includes(c)) { + i = d[i]; + x = -x; + } else { + ans.push(c); + } + i += x; + } + return ans.join(''); +} +``` + +#### JavaScript + +```js +/** + * @param {string} s + * @return {string} + */ +var reverseParentheses = function (s) { + const n = s.length; + const d = Array(n).fill(0); + const stk = []; + for (let i = 0; i < n; ++i) { + if (s[i] === '(') { + stk.push(i); + } else if (s[i] === ')') { + const j = stk.pop(); + d[i] = j; + d[j] = i; + } + } + let i = 0; + let x = 1; + const ans = []; + while (i < n) { + const c = s.charAt(i); + if ('()'.includes(c)) { + i = d[i]; + x = -x; + } else { + ans.push(c); + } + i += x; + } + return ans.join(''); +}; +``` + diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.java b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.java index 8a1e4cdd0febd..6c026fc98e8a5 100644 --- a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.java +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.java @@ -1,28 +1,19 @@ class Solution { public String reverseParentheses(String s) { - int n = s.length(); - int[] d = new int[n]; - Deque stk = new ArrayDeque<>(); - for (int i = 0; i < n; ++i) { - if (s.charAt(i) == '(') { - stk.push(i); - } else if (s.charAt(i) == ')') { - int j = stk.pop(); - d[i] = j; - d[j] = i; - } - } - StringBuilder ans = new StringBuilder(); - int i = 0, x = 1; - while (i < n) { - if (s.charAt(i) == '(' || s.charAt(i) == ')') { - i = d[i]; - x = -x; + StringBuilder stk = new StringBuilder(); + for (char c : s.toCharArray()) { + if (c == ')') { + StringBuilder t = new StringBuilder(); + while (stk.charAt(stk.length() - 1) != '(') { + t.append(stk.charAt(stk.length() - 1)); + stk.deleteCharAt(stk.length() - 1); + } + stk.deleteCharAt(stk.length() - 1); + stk.append(t); } else { - ans.append(s.charAt(i)); + stk.append(c); } - i += x; } - return ans.toString(); + return stk.toString(); } -} \ No newline at end of file +} diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.js b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.js index 8bfeb64750670..15c60516dc61a 100644 --- a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.js +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.js @@ -3,30 +3,18 @@ * @return {string} */ var reverseParentheses = function (s) { - const n = s.length; - const d = new Array(n).fill(0); const stk = []; - for (let i = 0; i < n; ++i) { - if (s[i] == '(') { - stk.push(i); - } else if (s[i] == ')') { - const j = stk.pop(); - d[i] = j; - d[j] = i; - } - } - let i = 0; - let x = 1; - const ans = []; - while (i < n) { - const c = s.charAt(i); - if (c == '(' || c == ')') { - i = d[i]; - x = -x; + for (const c of s) { + if (c === ')') { + const t = []; + while (stk.at(-1) !== '(') { + t.push(stk.pop()); + } + stk.pop(); + stk.push(...t); } else { - ans.push(c); + stk.push(c); } - i += x; } - return ans.join(''); + return stk.join(''); }; diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.py b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.py index 7ae106bbea4a0..1fda300c41f2e 100644 --- a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.py +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.py @@ -2,12 +2,12 @@ class Solution: def reverseParentheses(self, s: str) -> str: stk = [] for c in s: - if c == ')': + if c == ")": t = [] - while stk[-1] != '(': + while stk[-1] != "(": t.append(stk.pop()) stk.pop() stk.extend(t) else: stk.append(c) - return ''.join(stk) + return "".join(stk) diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.ts b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.ts new file mode 100644 index 0000000000000..3cd893f9a507d --- /dev/null +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.ts @@ -0,0 +1,16 @@ +function reverseParentheses(s: string): string { + const stk: string[] = []; + for (const c of s) { + if (c === ')') { + const t: string[] = []; + while (stk.at(-1)! !== '(') { + t.push(stk.pop()!); + } + stk.pop(); + stk.push(...t); + } else { + stk.push(c); + } + } + return stk.join(''); +} diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.java b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.java new file mode 100644 index 0000000000000..aba36d74fc0ce --- /dev/null +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.java @@ -0,0 +1,28 @@ +class Solution { + public String reverseParentheses(String s) { + int n = s.length(); + int[] d = new int[n]; + Deque stk = new ArrayDeque<>(); + for (int i = 0; i < n; ++i) { + if (s.charAt(i) == '(') { + stk.push(i); + } else if (s.charAt(i) == ')') { + int j = stk.pop(); + d[i] = j; + d[j] = i; + } + } + StringBuilder ans = new StringBuilder(); + int i = 0, x = 1; + while (i < n) { + if (s.charAt(i) == '(' || s.charAt(i) == ')') { + i = d[i]; + x = -x; + } else { + ans.append(s.charAt(i)); + } + i += x; + } + return ans.toString(); + } +} diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.js b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.js new file mode 100644 index 0000000000000..5cec3ddb5f95e --- /dev/null +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.js @@ -0,0 +1,32 @@ +/** + * @param {string} s + * @return {string} + */ +var reverseParentheses = function (s) { + const n = s.length; + const d = Array(n).fill(0); + const stk = []; + for (let i = 0; i < n; ++i) { + if (s[i] === '(') { + stk.push(i); + } else if (s[i] === ')') { + const j = stk.pop(); + d[i] = j; + d[j] = i; + } + } + let i = 0; + let x = 1; + const ans = []; + while (i < n) { + const c = s.charAt(i); + if ('()'.includes(c)) { + i = d[i]; + x = -x; + } else { + ans.push(c); + } + i += x; + } + return ans.join(''); +}; diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.py b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.py index dbd8e08776a9b..38916c562da31 100644 --- a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.py +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.py @@ -4,18 +4,18 @@ def reverseParentheses(self, s: str) -> str: d = [0] * n stk = [] for i, c in enumerate(s): - if c == '(': + if c == "(": stk.append(i) - elif c == ')': + elif c == ")": j = stk.pop() d[i], d[j] = j, i i, x = 0, 1 ans = [] while i < n: - if s[i] in '()': + if s[i] in "()": i = d[i] x = -x else: ans.append(s[i]) i += x - return ''.join(ans) + return "".join(ans) diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.ts b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.ts new file mode 100644 index 0000000000000..dd18a93202384 --- /dev/null +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.ts @@ -0,0 +1,28 @@ +function reverseParentheses(s: string): string { + const n = s.length; + const d: number[] = Array(n).fill(0); + const stk: number[] = []; + for (let i = 0; i < n; ++i) { + if (s[i] === '(') { + stk.push(i); + } else if (s[i] === ')') { + const j = stk.pop()!; + d[i] = j; + d[j] = i; + } + } + let i = 0; + let x = 1; + const ans: string[] = []; + while (i < n) { + const c = s.charAt(i); + if ('()'.includes(c)) { + i = d[i]; + x = -x; + } else { + ans.push(c); + } + i += x; + } + return ans.join(''); +}