LeetCode 题解工作台
替换字符串中的括号内容
给你一个字符串 s ,它包含一些括号对,每个括号中包含一个 非空 的键。 比方说,字符串 "(name)is(age)yearsold" 中,有 两个 括号对,分别包含键 "name" 和 "age" 。 你知道许多键对应的值,这些关系由二维字符串数组 knowledge 表示,其中 knowled…
3
题型
6
代码语言
3
相关题
当前训练重点
中等 · 数组·哈希·扫描
答案摘要
我们先用哈希表 记录 `knowledge` 中的键值对。 然后遍历字符串 ,如果当前字符是左括号 `'('`,则从当前位置开始向后遍历,直到遇到右括号 `')'`,此时括号内的字符串即为键,我们在哈希表 中查找该键对应的值,如果找到了,则将该值替换到括号内,否则替换为 `'?'`。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路
题目描述
给你一个字符串 s ,它包含一些括号对,每个括号中包含一个 非空 的键。
- 比方说,字符串
"(name)is(age)yearsold"中,有 两个 括号对,分别包含键"name"和"age"。
你知道许多键对应的值,这些关系由二维字符串数组 knowledge 表示,其中 knowledge[i] = [keyi, valuei] ,表示键 keyi 对应的值为 valuei 。
你需要替换 所有 的括号对。当你替换一个括号对,且它包含的键为 keyi 时,你需要:
- 将
keyi和括号用对应的值valuei替换。 - 如果从
knowledge中无法得知某个键对应的值,你需要将keyi和括号用问号"?"替换(不需要引号)。
knowledge 中每个键最多只会出现一次。s 中不会有嵌套的括号。
请你返回替换 所有 括号对后的结果字符串。
示例 1:
输入:s = "(name)is(age)yearsold", knowledge = [["name","bob"],["age","two"]] 输出:"bobistwoyearsold" 解释: 键 "name" 对应的值为 "bob" ,所以将 "(name)" 替换为 "bob" 。 键 "age" 对应的值为 "two" ,所以将 "(age)" 替换为 "two" 。
示例 2:
输入:s = "hi(name)", knowledge = [["a","b"]] 输出:"hi?" 解释:由于不知道键 "name" 对应的值,所以用 "?" 替换 "(name)" 。
示例 3:
输入:s = "(a)(a)(a)aaa", knowledge = [["a","yes"]] 输出:"yesyesyesaaa" 解释:相同的键在 s 中可能会出现多次。 键 "a" 对应的值为 "yes" ,所以将所有的 "(a)" 替换为 "yes" 。 注意,不在括号里的 "a" 不需要被替换。
提示:
1 <= s.length <= 1050 <= knowledge.length <= 105knowledge[i].length == 21 <= keyi.length, valuei.length <= 10s只包含小写英文字母和圆括号'('和')'。s中每一个左圆括号'('都有对应的右圆括号')'。s中每对括号内的键都不会为空。s中不会有嵌套括号对。keyi和valuei只包含小写英文字母。knowledge中的keyi不会重复。
解题思路
方法一:哈希表 + 模拟
我们先用哈希表 记录 knowledge 中的键值对。
然后遍历字符串 ,如果当前字符是左括号 '(',则从当前位置开始向后遍历,直到遇到右括号 ')',此时括号内的字符串即为键,我们在哈希表 中查找该键对应的值,如果找到了,则将该值替换到括号内,否则替换为 '?'。
时间复杂度 ,空间复杂度 。其中 和 分别为字符串 和列表 knowledge 的长度,而 为 knowledge 中所有字符串的长度之和。
class Solution:
def evaluate(self, s: str, knowledge: List[List[str]]) -> str:
d = {a: b for a, b in knowledge}
i, n = 0, len(s)
ans = []
while i < n:
if s[i] == '(':
j = s.find(')', i + 1)
ans.append(d.get(s[i + 1 : j], '?'))
i = j
else:
ans.append(s[i])
i += 1
return ''.join(ans)
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity is O(n + k) where n is the length of s and k is the number of knowledge entries, due to the single scan and hash table lookup. Space complexity is O(k + n) for the hash map and the resulting string construction. |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Check for missing keys and replace them with '?' consistently.
- question_mark
Repeated keys can appear multiple times; ask how to handle duplicates efficiently.
- question_mark
Brackets are guaranteed to be non-nested, simplifying linear scanning.
常见陷阱
外企场景- error
Forgetting to replace unknown keys with '?' instead of leaving brackets intact.
- error
Rescanning the knowledge array for each bracket instead of using a hash map.
- error
Not handling multiple occurrences of the same key correctly.
进阶变体
外企场景- arrow_right_alt
Evaluate strings where brackets may contain multi-character keys or numbers.
- arrow_right_alt
Process strings with larger knowledge sets requiring optimized hash map construction.
- arrow_right_alt
Handle cases where keys appear consecutively or interleaved with non-bracket characters.