LeetCode-1807 替换字符串中的括号内容

题目链接:1807. 替换字符串中的括号内容 - 力扣(LeetCode)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution(object):
def evaluate(self, s, knowledge):
"""
:type s: str
:type knowledge: List[List[str]]
:rtype: str
"""
dict_know = {k:v for k,v in knowledge} # 将二维列表转换为字典
new_s = s.replace('(','$').replace(')', '$') # 转换成便于处理的新格式

tokens = new_s.split('$')
for i in range(1, len(tokens), 2):
key = tokens[i]
tokens[i] = dict_know.get(key, "?")

return "".join(tokens)