We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
给你一个字符串s,它包含一些括号对,每个括号中包含一个 非空 的键。
s
比方说,字符串"(name)is(age)yearsold"中,有 两个 括号对,分别包含键"name"和"age"。 你知道许多键对应的值,这些关系由二维字符串数组knowledge表示,其中knowledge[i] = [keyi, valuei],表示键keyi对应的值为valuei。
"(name)is(age)yearsold"
"name"
"age"
knowledge
knowledge[i] = [keyi, valuei]
keyi
valuei
你需要替换 所有 的括号对。当你替换一个括号对,且它包含的键为keyi时,你需要:
将keyi和括号用对应的值valuei替换。 如果从knowledge中无法得知某个键对应的值,你需要将keyi和括号用问号"?"替换(不需要引号)。 knowledge中每个键最多只会出现一次,s中不会有嵌套的括号。
"?"
请你返回替换 所有 括号对后的结果字符串。
输入:s = "(name)is(age)yearsold", knowledge = [["name","bob"],["age","two"]] 输出:"bobistwoyearsold" 解释: 键 "name" 对应的值为 "bob" ,所以将 "(name)" 替换为 "bob" 。 键 "age" 对应的值为 "two" ,所以将 "(age)" 替换为 "two" 。
输入:s = "hi(name)", knowledge = [["a","b"]] 输出:"hi?" 解释:由于不知道键 "name" 对应的值,所以用 "?" 替换 "(name)" 。
输入:s = "(a)(a)(a)aaa", knowledge = [["a","yes"]] 输出:"yesyesyesaaa" 解释:相同的键在 s 中可能会出现多次。 键 "a" 对应的值为 "yes" ,所以将所有的 "(a)" 替换为 "yes" 。 注意,不在括号里的 "a" 不需要被替换。
输入:s = "(a)(b)", knowledge = [["a","b"],["b","a"]] 输出:"ba"
The text was updated successfully, but these errors were encountered:
func evaluate(s string, knowledge [][]string) string { dict := make(map[string]string, len(knowledge)) for _, v := range knowledge { dict[v[0]] = v[1] } re := regexp.MustCompile(`\([a-z]+\)`) return re.ReplaceAllStringFunc(s, func (substr string) string { substr = strings.Trim(substr, "(") substr = strings.Trim(substr, ")") if v, e := dict[substr]; e { return v } else { return "?" } }) }
Sorry, something went wrong.
func evaluate(s string, knowledge [][]string) string { var ret strings.Builder dict := make(map[string]string, len(knowledge)) for _, v := range knowledge { dict[v[0]] = v[1] } stack := []byte{} for i := 0; i < len(s); i++ { if s[i] == '(' { stack = append(stack, s[i]) } else if s[i] == ')' { str := string(stack[1:]) if v, e := dict[str]; e { ret.WriteString(v) } else { ret.WriteString("?") } stack = []byte{} } else if s[i] != '(' && len(stack) > 0{ stack = append(stack, s[i]) } else { ret.WriteByte(s[i]) } } return ret.String() }
No branches or pull requests
给你一个字符串
s
,它包含一些括号对,每个括号中包含一个 非空 的键。比方说,字符串
"(name)is(age)yearsold"
中,有 两个 括号对,分别包含键"name"
和"age"
。你知道许多键对应的值,这些关系由二维字符串数组
knowledge
表示,其中knowledge[i] = [keyi, valuei]
,表示键keyi
对应的值为valuei
。你需要替换 所有 的括号对。当你替换一个括号对,且它包含的键为
keyi
时,你需要:将
keyi
和括号用对应的值valuei
替换。如果从
knowledge
中无法得知某个键对应的值,你需要将keyi
和括号用问号"?"
替换(不需要引号)。knowledge
中每个键最多只会出现一次,s
中不会有嵌套的括号。请你返回替换 所有 括号对后的结果字符串。
示例 1:
示例 2:
示例 3:
示例 4:
提示:
The text was updated successfully, but these errors were encountered: