LeetCode 题解工作台
判断一个括号字符串是否有效
一个括号字符串是只由 '(' 和 ')' 组成的 非空 字符串。如果一个字符串满足下面 任意 一个条件,那么它就是有效的: 字符串为 () . 它可以表示为 AB ( A 与 B 连接),其中 A 和 B 都是有效括号字符串。 它可以表示为 (A) ,其中 A 是一个有效括号字符串。 给你一个括号字…
3
题型
5
代码语言
3
相关题
当前训练重点
中等 · 栈·状态
答案摘要
我们观察发现,奇数长度的字符串一定不是有效的括号字符串,因为无论怎么匹配,都会剩下一个括号。因此,如果字符串 的长度是奇数,提前返回 。 接下来,我们进行两次遍历。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 栈·状态 题型思路
题目描述
一个括号字符串是只由 '(' 和 ')' 组成的 非空 字符串。如果一个字符串满足下面 任意 一个条件,那么它就是有效的:
- 字符串为
(). - 它可以表示为
AB(A与B连接),其中A和B都是有效括号字符串。 - 它可以表示为
(A),其中A是一个有效括号字符串。
给你一个括号字符串 s 和一个字符串 locked ,两者长度都为 n 。locked 是一个二进制字符串,只包含 '0' 和 '1' 。对于 locked 中 每一个 下标 i :
- 如果
locked[i]是'1',你 不能 改变s[i]。 - 如果
locked[i]是'0',你 可以 将s[i]变为'('或者')'。
如果你可以将 s 变为有效括号字符串,请你返回 true ,否则返回 false 。
示例 1:

输入:s = "))()))", locked = "010100"
输出:true
解释:locked[1] == '1' 和 locked[3] == '1' ,所以我们无法改变 s[1] 或者 s[3] 。
我们可以将 s[0] 和 s[4] 变为 '(' ,不改变 s[2] 和 s[5] ,使 s 变为有效字符串。
示例 2:
输入:s = "()()", locked = "0000" 输出:true 解释:我们不需要做任何改变,因为 s 已经是有效字符串了。
示例 3:
输入:s = ")", locked = "0"
输出:false
解释:locked 允许改变 s[0] 。
但无论将 s[0] 变为 '(' 或者 ')' 都无法使 s 变为有效字符串。
示例 4:
输入:s = "(((())(((())", locked = "111111010111" 输出:true 解释:locked 允许我们改变 s[6] 和 s[8]。 我们将 s[6] 和 s[8] 改为 ')' 使 s 变为有效字符串。
提示:
n == s.length == locked.length1 <= n <= 105s[i]要么是'('要么是')'。locked[i]要么是'0'要么是'1'。
解题思路
方法一:贪心 + 两次遍历
我们观察发现,奇数长度的字符串一定不是有效的括号字符串,因为无论怎么匹配,都会剩下一个括号。因此,如果字符串 的长度是奇数,提前返回 。
接下来,我们进行两次遍历。
第一次从左到右,判断所有的 '(' 括号是否可以被 ')' 或者可变括号匹配,如果不可以,直接返回 。
第二次从右到左,判断所有的 ')' 括号是否可以被 '(' 或者可变括号匹配,如果不可以,直接返回 。
遍历结束,说明所有的括号都可以被匹配,字符串 是有效的括号字符串,返回 。
时间复杂度 ,其中 为字符串 的长度。空间复杂度 。
相似题目:
class Solution:
def canBeValid(self, s: str, locked: str) -> bool:
n = len(s)
if n & 1:
return False
x = 0
for i in range(n):
if s[i] == '(' or locked[i] == '0':
x += 1
elif x:
x -= 1
else:
return False
x = 0
for i in range(n - 1, -1, -1):
if s[i] == ')' or locked[i] == '0':
x += 1
elif x:
x -= 1
else:
return False
return True
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity is O(n) since each character is processed in both left-to-right and right-to-left passes. Space complexity is O(1) using counters instead of an actual stack. |
| 空间 | O(1) |
面试官常问的追问
外企场景- question_mark
Can you check if the string length is odd as a quick fail case?
- question_mark
How would you track balance considering locked and unlocked positions efficiently?
- question_mark
Can you simulate stack behavior without actually using extra space?
常见陷阱
外企场景- error
Assuming all unlocked characters can be freely chosen without considering positions of locked characters.
- error
Ignoring the need to check balance in both directions, which can miss invalid strings ending with extra '('.
- error
Forgetting that strings of odd length can never be valid.
进阶变体
外企场景- arrow_right_alt
Allowing multiple types of brackets like '[]' and '{}' with the same locked rules.
- arrow_right_alt
Counting the minimum changes needed to make the string valid instead of just returning true/false.
- arrow_right_alt
Checking validity when some locked positions allow only a subset of possible parentheses types.