Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 22ce977

Browse files
feat: add solutions to lc problem: No.2116 (#4132)
No.2116.Check if a Parentheses String Can Be Valid
1 parent f65ceab commit 22ce977

File tree

7 files changed

+168
-190
lines changed

7 files changed

+168
-190
lines changed

‎solution/2100-2199/2116.Check if a Parentheses String Can Be Valid/README.md‎

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,17 @@ tags:
9494

9595
### 方法一:贪心 + 两次遍历
9696

97-
我们观察发现,奇数长度的字符串一定不是有效的括号字符串,因为无论怎么匹配,都会剩下一个括号。因此,如果字符串 $s$ 的长度是奇数,提前返回 `false`
97+
我们观察发现,奇数长度的字符串一定不是有效的括号字符串,因为无论怎么匹配,都会剩下一个括号。因此,如果字符串 $s$ 的长度是奇数,提前返回 $\textit{false}$
9898

9999
接下来,我们进行两次遍历。
100100

101-
第一次从左到右,判断所有的 `'('` 括号是否可以被 `')'` 或者可变括号匹配,如果不可以,直接返回 `false`
101+
第一次从左到右,判断所有的 `'('` 括号是否可以被 `')'` 或者可变括号匹配,如果不可以,直接返回 $\textit{false}$
102102

103-
第二次从右到左,判断所有的 `')'` 括号是否可以被 `'('` 或者可变括号匹配,如果不可以,直接返回 `false`
103+
第二次从右到左,判断所有的 `')'` 括号是否可以被 `'('` 或者可变括号匹配,如果不可以,直接返回 $\textit{false}$
104104

105-
遍历结束,说明所有的括号都可以被匹配,字符串 $s$ 是有效的括号字符串,返回 `true`
105+
遍历结束,说明所有的括号都可以被匹配,字符串 $s$ 是有效的括号字符串,返回 $\textit{true}$
106106

107-
时间复杂度 $O(n),ドル空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。
107+
时间复杂度 $O(n),ドル其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(1)$
108108

109109
相似题目:
110110

@@ -240,6 +240,38 @@ func canBeValid(s string, locked string) bool {
240240
}
241241
```
242242

243+
#### TypeScript
244+
245+
```ts
246+
function canBeValid(s: string, locked: string): boolean {
247+
const n = s.length;
248+
if (n & 1) {
249+
return false;
250+
}
251+
let x = 0;
252+
for (let i = 0; i < n; ++i) {
253+
if (s[i] === '(' || locked[i] === '0') {
254+
++x;
255+
} else if (x > 0) {
256+
--x;
257+
} else {
258+
return false;
259+
}
260+
}
261+
x = 0;
262+
for (let i = n - 1; i >= 0; --i) {
263+
if (s[i] === ')' || locked[i] === '0') {
264+
++x;
265+
} else if (x > 0) {
266+
--x;
267+
} else {
268+
return false;
269+
}
270+
}
271+
return true;
272+
}
273+
```
274+
243275
<!-- tabs:end -->
244276

245277
<!-- solution:end -->

‎solution/2100-2199/2116.Check if a Parentheses String Can Be Valid/README_EN.md‎

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ We change s[0] and s[4] to &#39;(&#39; while leaving s[2] and s[5] unchanged to
5959
<pre>
6060
<strong>Input:</strong> s = &quot;)&quot;, locked = &quot;0&quot;
6161
<strong>Output:</strong> false
62-
<strong>Explanation:</strong> locked permits us to change s[0].
62+
<strong>Explanation:</strong> locked permits us to change s[0].
6363
Changing s[0] to either &#39;(&#39; or &#39;)&#39; will not make s valid.
6464
</pre>
6565

@@ -68,7 +68,7 @@ Changing s[0] to either &#39;(&#39; or &#39;)&#39; will not make s valid.
6868
<pre>
6969
<strong>Input:</strong> s = &quot;(((())(((())&quot;, locked = &quot;111111010111&quot;
7070
<strong>Output:</strong> false
71-
<strong>Explanation:</strong> locked permits us to change s[6] and s[8].
71+
<strong>Explanation:</strong> locked permits us to change s[6] and s[8].
7272
We change s[6] and s[8] to &#39;)&#39; to make s valid.
7373
</pre>
7474

@@ -88,7 +88,23 @@ We change s[6] and s[8] to &#39;)&#39; to make s valid.
8888

8989
<!-- solution:start -->
9090

91-
### Solution 1
91+
### Solution 1: Greedy + Two Passes
92+
93+
We observe that a string of odd length cannot be a valid parentheses string because there will always be one unmatched parenthesis. Therefore, if the length of the string $s$ is odd, return $\textit{false}$ immediately.
94+
95+
Next, we perform two passes.
96+
97+
The first pass goes from left to right, checking if all `'('` parentheses can be matched by `')'` or changeable parentheses. If not, return $\textit{false}$.
98+
99+
The second pass goes from right to left, checking if all `')'` parentheses can be matched by `'('` or changeable parentheses. If not, return $\textit{false}$.
100+
101+
If both passes complete successfully, it means all parentheses can be matched, and the string $s$ is a valid parentheses string. Return $\textit{true}$.
102+
103+
The time complexity is $O(n),ドル where $n$ is the length of the string $s$. The space complexity is $O(1)$.
104+
105+
Similar problems:
106+
107+
- [678. Valid Parenthesis String](https://github.com/doocs/leetcode/blob/main/solution/0600-0699/0678.Valid%20Parenthesis%20String/README_EN.md)
92108

93109
<!-- tabs:start -->
94110

@@ -220,6 +236,38 @@ func canBeValid(s string, locked string) bool {
220236
}
221237
```
222238

239+
#### TypeScript
240+
241+
```ts
242+
function canBeValid(s: string, locked: string): boolean {
243+
const n = s.length;
244+
if (n & 1) {
245+
return false;
246+
}
247+
let x = 0;
248+
for (let i = 0; i < n; ++i) {
249+
if (s[i] === '(' || locked[i] === '0') {
250+
++x;
251+
} else if (x > 0) {
252+
--x;
253+
} else {
254+
return false;
255+
}
256+
}
257+
x = 0;
258+
for (let i = n - 1; i >= 0; --i) {
259+
if (s[i] === ')' || locked[i] === '0') {
260+
++x;
261+
} else if (x > 0) {
262+
--x;
263+
} else {
264+
return false;
265+
}
266+
}
267+
return true;
268+
}
269+
```
270+
223271
<!-- tabs:end -->
224272

225273
<!-- solution:end -->
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function canBeValid(s: string, locked: string): boolean {
2+
const n = s.length;
3+
if (n & 1) {
4+
return false;
5+
}
6+
let x = 0;
7+
for (let i = 0; i < n; ++i) {
8+
if (s[i] === '(' || locked[i] === '0') {
9+
++x;
10+
} else if (x > 0) {
11+
--x;
12+
} else {
13+
return false;
14+
}
15+
}
16+
x = 0;
17+
for (let i = n - 1; i >= 0; --i) {
18+
if (s[i] === ')' || locked[i] === '0') {
19+
++x;
20+
} else if (x > 0) {
21+
--x;
22+
} else {
23+
return false;
24+
}
25+
}
26+
return true;
27+
}

‎solution/2100-2199/2117.Abbreviating the Product of a Range/README.md‎

Lines changed: 19 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ tags:
6464
<strong>输入:</strong>left = 2, right = 11
6565
<strong>输出:</strong>"399168e2"
6666
<strong>解释:</strong>乘积为 39916800 。
67-
有 2 个后缀 0 ,删除后得到 399168 。缩写的结尾为 "e2" 。
68-
删除后缀 0 后是 6 位数,不需要进一步缩写。
67+
有 2 个后缀 0 ,删除后得到 399168 。缩写的结尾为 "e2" 。
68+
删除后缀 0 后是 6 位数,不需要进一步缩写。
6969
所以,最终将乘积表示为 "399168e2" 。
7070
</pre>
7171

@@ -98,39 +98,36 @@ tags:
9898
#### Python3
9999

100100
```python
101-
import numpy
102-
103-
104101
class Solution:
105102
def abbreviateProduct(self, left: int, right: int) -> str:
106103
cnt2 = cnt5 = 0
107-
z = numpy.float128(0)
108104
for x in range(left, right + 1):
109-
z += numpy.log10(x)
110105
while x % 2 == 0:
111-
x //= 2
112106
cnt2 += 1
107+
x //= 2
113108
while x % 5 == 0:
114-
x //= 5
115109
cnt5 += 1
110+
x //= 5
116111
c = cnt2 = cnt5 = min(cnt2, cnt5)
117-
suf = y = 1
112+
pre = suf = 1
118113
gt = False
119114
for x in range(left, right + 1):
120-
while cnt2 and x % 2 == 0:
121-
x //= 2
115+
suf *= x
116+
while cnt2 and suf % 2 == 0:
117+
suf //= 2
122118
cnt2 -= 1
123-
while cnt5 and x % 5 == 0:
124-
x //= 5
119+
while cnt5 and suf % 5 == 0:
120+
suf //= 5
125121
cnt5 -= 1
126-
suf = suf * x % 100000
127-
if not gt:
128-
y *= x
129-
gt = y >= 1e10
130-
if not gt:
131-
return str(y) + "e" + str(c)
132-
pre = int(pow(10, z - int(z) + 4))
133-
return str(pre) + "..." + str(suf).zfill(5) + "e" + str(c)
122+
if suf >= 1e10:
123+
gt = True
124+
suf %= int(1e10)
125+
pre *= x
126+
while pre > 1e5:
127+
pre /= 10
128+
if gt:
129+
return str(int(pre)) + "..." + str(suf % int(1e5)).zfill(5) + "e" + str(c)
130+
return str(suf) + "e" + str(c)
134131
```
135132

136133
#### Java
@@ -271,49 +268,4 @@ func abbreviateProduct(left int, right int) string {
271268

272269
<!-- solution:end -->
273270

274-
<!-- solution:start -->
275-
276-
### 方法二
277-
278-
<!-- tabs:start -->
279-
280-
#### Python3
281-
282-
```python
283-
class Solution:
284-
def abbreviateProduct(self, left: int, right: int) -> str:
285-
cnt2 = cnt5 = 0
286-
for x in range(left, right + 1):
287-
while x % 2 == 0:
288-
cnt2 += 1
289-
x //= 2
290-
while x % 5 == 0:
291-
cnt5 += 1
292-
x //= 5
293-
c = cnt2 = cnt5 = min(cnt2, cnt5)
294-
pre = suf = 1
295-
gt = False
296-
for x in range(left, right + 1):
297-
suf *= x
298-
while cnt2 and suf % 2 == 0:
299-
suf //= 2
300-
cnt2 -= 1
301-
while cnt5 and suf % 5 == 0:
302-
suf //= 5
303-
cnt5 -= 1
304-
if suf >= 1e10:
305-
gt = True
306-
suf %= int(1e10)
307-
pre *= x
308-
while pre > 1e5:
309-
pre /= 10
310-
if gt:
311-
return str(int(pre)) + "..." + str(suf % int(1e5)).zfill(5) + 'e' + str(c)
312-
return str(suf) + "e" + str(c)
313-
```
314-
315-
<!-- tabs:end -->
316-
317-
<!-- solution:end -->
318-
319271
<!-- problem:end -->

‎solution/2100-2199/2117.Abbreviating the Product of a Range/README_EN.md‎

Lines changed: 17 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -95,39 +95,36 @@ Hence, the abbreviated product is &quot;399168e2&quot;.
9595
#### Python3
9696

9797
```python
98-
import numpy
99-
100-
10198
class Solution:
10299
def abbreviateProduct(self, left: int, right: int) -> str:
103100
cnt2 = cnt5 = 0
104-
z = numpy.float128(0)
105101
for x in range(left, right + 1):
106-
z += numpy.log10(x)
107102
while x % 2 == 0:
108-
x //= 2
109103
cnt2 += 1
104+
x //= 2
110105
while x % 5 == 0:
111-
x //= 5
112106
cnt5 += 1
107+
x //= 5
113108
c = cnt2 = cnt5 = min(cnt2, cnt5)
114-
suf = y = 1
109+
pre = suf = 1
115110
gt = False
116111
for x in range(left, right + 1):
117-
while cnt2 and x % 2 == 0:
118-
x //= 2
112+
suf *= x
113+
while cnt2 and suf % 2 == 0:
114+
suf //= 2
119115
cnt2 -= 1
120-
while cnt5 and x % 5 == 0:
121-
x //= 5
116+
while cnt5 and suf % 5 == 0:
117+
suf //= 5
122118
cnt5 -= 1
123-
suf = suf * x % 100000
124-
if not gt:
125-
y *= x
126-
gt = y >= 1e10
127-
if not gt:
128-
return str(y) + "e" + str(c)
129-
pre = int(pow(10, z - int(z) + 4))
130-
return str(pre) + "..." + str(suf).zfill(5) + "e" + str(c)
119+
if suf >= 1e10:
120+
gt = True
121+
suf %= int(1e10)
122+
pre *= x
123+
while pre > 1e5:
124+
pre /= 10
125+
if gt:
126+
return str(int(pre)) + "..." + str(suf % int(1e5)).zfill(5) + "e" + str(c)
127+
return str(suf) + "e" + str(c)
131128
```
132129

133130
#### Java
@@ -268,49 +265,4 @@ func abbreviateProduct(left int, right int) string {
268265

269266
<!-- solution:end -->
270267

271-
<!-- solution:start -->
272-
273-
### Solution 2
274-
275-
<!-- tabs:start -->
276-
277-
#### Python3
278-
279-
```python
280-
class Solution:
281-
def abbreviateProduct(self, left: int, right: int) -> str:
282-
cnt2 = cnt5 = 0
283-
for x in range(left, right + 1):
284-
while x % 2 == 0:
285-
cnt2 += 1
286-
x //= 2
287-
while x % 5 == 0:
288-
cnt5 += 1
289-
x //= 5
290-
c = cnt2 = cnt5 = min(cnt2, cnt5)
291-
pre = suf = 1
292-
gt = False
293-
for x in range(left, right + 1):
294-
suf *= x
295-
while cnt2 and suf % 2 == 0:
296-
suf //= 2
297-
cnt2 -= 1
298-
while cnt5 and suf % 5 == 0:
299-
suf //= 5
300-
cnt5 -= 1
301-
if suf >= 1e10:
302-
gt = True
303-
suf %= int(1e10)
304-
pre *= x
305-
while pre > 1e5:
306-
pre /= 10
307-
if gt:
308-
return str(int(pre)) + "..." + str(suf % int(1e5)).zfill(5) + 'e' + str(c)
309-
return str(suf) + "e" + str(c)
310-
```
311-
312-
<!-- tabs:end -->
313-
314-
<!-- solution:end -->
315-
316268
<!-- problem:end -->

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /