diff --git a/solution/2100-2199/2116.Check if a Parentheses String Can Be Valid/README.md b/solution/2100-2199/2116.Check if a Parentheses String Can Be Valid/README.md index af2d8402764d3..206d66df1a11b 100644 --- a/solution/2100-2199/2116.Check if a Parentheses String Can Be Valid/README.md +++ b/solution/2100-2199/2116.Check if a Parentheses String Can Be Valid/README.md @@ -94,17 +94,17 @@ tags: ### 方法一:贪心 + 两次遍历 -我们观察发现,奇数长度的字符串一定不是有效的括号字符串,因为无论怎么匹配,都会剩下一个括号。因此,如果字符串 $s$ 的长度是奇数,提前返回 `false`。 +我们观察发现,奇数长度的字符串一定不是有效的括号字符串,因为无论怎么匹配,都会剩下一个括号。因此,如果字符串 $s$ 的长度是奇数,提前返回 $\textit{false}$。 接下来,我们进行两次遍历。 -第一次从左到右,判断所有的 `'('` 括号是否可以被 `')'` 或者可变括号匹配,如果不可以,直接返回 `false`。 +第一次从左到右,判断所有的 `'('` 括号是否可以被 `')'` 或者可变括号匹配,如果不可以,直接返回 $\textit{false}$。 -第二次从右到左,判断所有的 `')'` 括号是否可以被 `'('` 或者可变括号匹配,如果不可以,直接返回 `false`。 +第二次从右到左,判断所有的 `')'` 括号是否可以被 `'('` 或者可变括号匹配,如果不可以,直接返回 $\textit{false}$。 -遍历结束,说明所有的括号都可以被匹配,字符串 $s$ 是有效的括号字符串,返回 `true`。 +遍历结束,说明所有的括号都可以被匹配,字符串 $s$ 是有效的括号字符串,返回 $\textit{true}$。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。 +时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(1)$。 相似题目: @@ -240,6 +240,38 @@ func canBeValid(s string, locked string) bool { } ``` +#### TypeScript + +```ts +function canBeValid(s: string, locked: string): boolean { + const n = s.length; + if (n & 1) { + return false; + } + let x = 0; + for (let i = 0; i < n; ++i) { + if (s[i] === '(' || locked[i] === '0') { + ++x; + } else if (x > 0) { + --x; + } else { + return false; + } + } + x = 0; + for (let i = n - 1; i >= 0; --i) { + if (s[i] === ')' || locked[i] === '0') { + ++x; + } else if (x > 0) { + --x; + } else { + return false; + } + } + return true; +} +``` + diff --git a/solution/2100-2199/2116.Check if a Parentheses String Can Be Valid/README_EN.md b/solution/2100-2199/2116.Check if a Parentheses String Can Be Valid/README_EN.md index 14cd0970b1a5a..052deb6a373b1 100644 --- a/solution/2100-2199/2116.Check if a Parentheses String Can Be Valid/README_EN.md +++ b/solution/2100-2199/2116.Check if a Parentheses String Can Be Valid/README_EN.md @@ -59,7 +59,7 @@ We change s[0] and s[4] to '(' while leaving s[2] and s[5] unchanged to
Input: s = ")", locked = "0" Output: false -Explanation: locked permits us to change s[0]. +Explanation: locked permits us to change s[0]. Changing s[0] to either '(' or ')' will not make s valid.@@ -68,7 +68,7 @@ Changing s[0] to either '(' or ')' will not make s valid.
Input: s = "(((())(((())", locked = "111111010111" Output: false -Explanation: locked permits us to change s[6] and s[8]. +Explanation: locked permits us to change s[6] and s[8]. We change s[6] and s[8] to ')' to make s valid.@@ -88,7 +88,23 @@ We change s[6] and s[8] to ')' to make s valid. -### Solution 1 +### Solution 1: Greedy + Two Passes + +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. + +Next, we perform two passes. + +The first pass goes from left to right, checking if all `'('` parentheses can be matched by `')'` or changeable parentheses. If not, return $\textit{false}$. + +The second pass goes from right to left, checking if all `')'` parentheses can be matched by `'('` or changeable parentheses. If not, return $\textit{false}$. + +If both passes complete successfully, it means all parentheses can be matched, and the string $s$ is a valid parentheses string. Return $\textit{true}$. + +The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$. + +Similar problems: + +- [678. Valid Parenthesis String](https://github.com/doocs/leetcode/blob/main/solution/0600-0699/0678.Valid%20Parenthesis%20String/README_EN.md) @@ -220,6 +236,38 @@ func canBeValid(s string, locked string) bool { } ``` +#### TypeScript + +```ts +function canBeValid(s: string, locked: string): boolean { + const n = s.length; + if (n & 1) { + return false; + } + let x = 0; + for (let i = 0; i < n; ++i) { + if (s[i] === '(' || locked[i] === '0') { + ++x; + } else if (x > 0) { + --x; + } else { + return false; + } + } + x = 0; + for (let i = n - 1; i >= 0; --i) { + if (s[i] === ')' || locked[i] === '0') { + ++x; + } else if (x > 0) { + --x; + } else { + return false; + } + } + return true; +} +``` + diff --git a/solution/2100-2199/2116.Check if a Parentheses String Can Be Valid/Solution.ts b/solution/2100-2199/2116.Check if a Parentheses String Can Be Valid/Solution.ts new file mode 100644 index 0000000000000..5de094122bd22 --- /dev/null +++ b/solution/2100-2199/2116.Check if a Parentheses String Can Be Valid/Solution.ts @@ -0,0 +1,27 @@ +function canBeValid(s: string, locked: string): boolean { + const n = s.length; + if (n & 1) { + return false; + } + let x = 0; + for (let i = 0; i < n; ++i) { + if (s[i] === '(' || locked[i] === '0') { + ++x; + } else if (x > 0) { + --x; + } else { + return false; + } + } + x = 0; + for (let i = n - 1; i >= 0; --i) { + if (s[i] === ')' || locked[i] === '0') { + ++x; + } else if (x > 0) { + --x; + } else { + return false; + } + } + return true; +} diff --git a/solution/2100-2199/2117.Abbreviating the Product of a Range/README.md b/solution/2100-2199/2117.Abbreviating the Product of a Range/README.md index be7fca6ed0487..857b04034468a 100644 --- a/solution/2100-2199/2117.Abbreviating the Product of a Range/README.md +++ b/solution/2100-2199/2117.Abbreviating the Product of a Range/README.md @@ -64,8 +64,8 @@ tags: 输入:left = 2, right = 11 输出:"399168e2" 解释:乘积为 39916800 。 -有 2 个后缀 0 ,删除后得到 399168 。缩写的结尾为 "e2" 。 -删除后缀 0 后是 6 位数,不需要进一步缩写。 +有 2 个后缀 0 ,删除后得到 399168 。缩写的结尾为 "e2" 。 +删除后缀 0 后是 6 位数,不需要进一步缩写。 所以,最终将乘积表示为 "399168e2" 。 @@ -98,39 +98,36 @@ tags: #### Python3 ```python -import numpy - - class Solution: def abbreviateProduct(self, left: int, right: int) -> str: cnt2 = cnt5 = 0 - z = numpy.float128(0) for x in range(left, right + 1): - z += numpy.log10(x) while x % 2 == 0: - x //= 2 cnt2 += 1 + x //= 2 while x % 5 == 0: - x //= 5 cnt5 += 1 + x //= 5 c = cnt2 = cnt5 = min(cnt2, cnt5) - suf = y = 1 + pre = suf = 1 gt = False for x in range(left, right + 1): - while cnt2 and x % 2 == 0: - x //= 2 + suf *= x + while cnt2 and suf % 2 == 0: + suf //= 2 cnt2 -= 1 - while cnt5 and x % 5 == 0: - x //= 5 + while cnt5 and suf % 5 == 0: + suf //= 5 cnt5 -= 1 - suf = suf * x % 100000 - if not gt: - y *= x - gt = y >= 1e10 - if not gt: - return str(y) + "e" + str(c) - pre = int(pow(10, z - int(z) + 4)) - return str(pre) + "..." + str(suf).zfill(5) + "e" + str(c) + if suf >= 1e10: + gt = True + suf %= int(1e10) + pre *= x + while pre > 1e5: + pre /= 10 + if gt: + return str(int(pre)) + "..." + str(suf % int(1e5)).zfill(5) + "e" + str(c) + return str(suf) + "e" + str(c) ``` #### Java @@ -271,49 +268,4 @@ func abbreviateProduct(left int, right int) string { - - -### 方法二 - - - -#### Python3 - -```python -class Solution: - def abbreviateProduct(self, left: int, right: int) -> str: - cnt2 = cnt5 = 0 - for x in range(left, right + 1): - while x % 2 == 0: - cnt2 += 1 - x //= 2 - while x % 5 == 0: - cnt5 += 1 - x //= 5 - c = cnt2 = cnt5 = min(cnt2, cnt5) - pre = suf = 1 - gt = False - for x in range(left, right + 1): - suf *= x - while cnt2 and suf % 2 == 0: - suf //= 2 - cnt2 -= 1 - while cnt5 and suf % 5 == 0: - suf //= 5 - cnt5 -= 1 - if suf >= 1e10: - gt = True - suf %= int(1e10) - pre *= x - while pre > 1e5: - pre /= 10 - if gt: - return str(int(pre)) + "..." + str(suf % int(1e5)).zfill(5) + 'e' + str(c) - return str(suf) + "e" + str(c) -``` - - - - - diff --git a/solution/2100-2199/2117.Abbreviating the Product of a Range/README_EN.md b/solution/2100-2199/2117.Abbreviating the Product of a Range/README_EN.md index 4327a39cdbc67..be9f6b3940a95 100644 --- a/solution/2100-2199/2117.Abbreviating the Product of a Range/README_EN.md +++ b/solution/2100-2199/2117.Abbreviating the Product of a Range/README_EN.md @@ -95,39 +95,36 @@ Hence, the abbreviated product is "399168e2". #### Python3 ```python -import numpy - - class Solution: def abbreviateProduct(self, left: int, right: int) -> str: cnt2 = cnt5 = 0 - z = numpy.float128(0) for x in range(left, right + 1): - z += numpy.log10(x) while x % 2 == 0: - x //= 2 cnt2 += 1 + x //= 2 while x % 5 == 0: - x //= 5 cnt5 += 1 + x //= 5 c = cnt2 = cnt5 = min(cnt2, cnt5) - suf = y = 1 + pre = suf = 1 gt = False for x in range(left, right + 1): - while cnt2 and x % 2 == 0: - x //= 2 + suf *= x + while cnt2 and suf % 2 == 0: + suf //= 2 cnt2 -= 1 - while cnt5 and x % 5 == 0: - x //= 5 + while cnt5 and suf % 5 == 0: + suf //= 5 cnt5 -= 1 - suf = suf * x % 100000 - if not gt: - y *= x - gt = y >= 1e10 - if not gt: - return str(y) + "e" + str(c) - pre = int(pow(10, z - int(z) + 4)) - return str(pre) + "..." + str(suf).zfill(5) + "e" + str(c) + if suf >= 1e10: + gt = True + suf %= int(1e10) + pre *= x + while pre > 1e5: + pre /= 10 + if gt: + return str(int(pre)) + "..." + str(suf % int(1e5)).zfill(5) + "e" + str(c) + return str(suf) + "e" + str(c) ``` #### Java @@ -268,49 +265,4 @@ func abbreviateProduct(left int, right int) string { - - -### Solution 2 - - - -#### Python3 - -```python -class Solution: - def abbreviateProduct(self, left: int, right: int) -> str: - cnt2 = cnt5 = 0 - for x in range(left, right + 1): - while x % 2 == 0: - cnt2 += 1 - x //= 2 - while x % 5 == 0: - cnt5 += 1 - x //= 5 - c = cnt2 = cnt5 = min(cnt2, cnt5) - pre = suf = 1 - gt = False - for x in range(left, right + 1): - suf *= x - while cnt2 and suf % 2 == 0: - suf //= 2 - cnt2 -= 1 - while cnt5 and suf % 5 == 0: - suf //= 5 - cnt5 -= 1 - if suf >= 1e10: - gt = True - suf %= int(1e10) - pre *= x - while pre > 1e5: - pre /= 10 - if gt: - return str(int(pre)) + "..." + str(suf % int(1e5)).zfill(5) + 'e' + str(c) - return str(suf) + "e" + str(c) -``` - - - - - diff --git a/solution/2100-2199/2117.Abbreviating the Product of a Range/Solution.py b/solution/2100-2199/2117.Abbreviating the Product of a Range/Solution.py index 1473b889d3756..36a531c4c1860 100644 --- a/solution/2100-2199/2117.Abbreviating the Product of a Range/Solution.py +++ b/solution/2100-2199/2117.Abbreviating the Product of a Range/Solution.py @@ -1,33 +1,30 @@ -import numpy - - class Solution: def abbreviateProduct(self, left: int, right: int) -> str: cnt2 = cnt5 = 0 - z = numpy.float128(0) for x in range(left, right + 1): - z += numpy.log10(x) while x % 2 == 0: - x //= 2 cnt2 += 1 + x //= 2 while x % 5 == 0: - x //= 5 cnt5 += 1 + x //= 5 c = cnt2 = cnt5 = min(cnt2, cnt5) - suf = y = 1 + pre = suf = 1 gt = False for x in range(left, right + 1): - while cnt2 and x % 2 == 0: - x //= 2 + suf *= x + while cnt2 and suf % 2 == 0: + suf //= 2 cnt2 -= 1 - while cnt5 and x % 5 == 0: - x //= 5 + while cnt5 and suf % 5 == 0: + suf //= 5 cnt5 -= 1 - suf = suf * x % 100000 - if not gt: - y *= x - gt = y >= 1e10 - if not gt: - return str(y) + "e" + str(c) - pre = int(pow(10, z - int(z) + 4)) - return str(pre) + "..." + str(suf).zfill(5) + "e" + str(c) + if suf >= 1e10: + gt = True + suf %= int(1e10) + pre *= x + while pre > 1e5: + pre /= 10 + if gt: + return str(int(pre)) + "..." + str(suf % int(1e5)).zfill(5) + "e" + str(c) + return str(suf) + "e" + str(c) diff --git a/solution/2100-2199/2117.Abbreviating the Product of a Range/Solution2.py b/solution/2100-2199/2117.Abbreviating the Product of a Range/Solution2.py deleted file mode 100644 index 22b78d5705115..0000000000000 --- a/solution/2100-2199/2117.Abbreviating the Product of a Range/Solution2.py +++ /dev/null @@ -1,30 +0,0 @@ -class Solution: - def abbreviateProduct(self, left: int, right: int) -> str: - cnt2 = cnt5 = 0 - for x in range(left, right + 1): - while x % 2 == 0: - cnt2 += 1 - x //= 2 - while x % 5 == 0: - cnt5 += 1 - x //= 5 - c = cnt2 = cnt5 = min(cnt2, cnt5) - pre = suf = 1 - gt = False - for x in range(left, right + 1): - suf *= x - while cnt2 and suf % 2 == 0: - suf //= 2 - cnt2 -= 1 - while cnt5 and suf % 5 == 0: - suf //= 5 - cnt5 -= 1 - if suf >= 1e10: - gt = True - suf %= int(1e10) - pre *= x - while pre > 1e5: - pre /= 10 - if gt: - return str(int(pre)) + "..." + str(suf % int(1e5)).zfill(5) + 'e' + str(c) - return str(suf) + "e" + str(c)