Skip to content
New issue

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

替换字符串中的括号内容 #128

Open
yankewei opened this issue Mar 28, 2021 · 2 comments
Open

替换字符串中的括号内容 #128

yankewei opened this issue Mar 28, 2021 · 2 comments
Labels
中等 题目难度为中等

Comments

@yankewei
Copy link
Owner

给你一个字符串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" 不需要被替换。

示例 4:

输入:s = "(a)(b)", knowledge = [["a","b"],["b","a"]]
输出:"ba"

提示:

  • 1 <= s.length <= 105
  • 0 <= knowledge.length <= 105
  • knowledge[i].length == 2
  • 1 <= keyi.length, valuei.length <= 10
  • s 只包含小写英文字母和圆括号 '(' 和 ')' 。
  • s 中每一个左圆括号 '(' 都有对应的右圆括号 ')' 。
  • s 中每对括号内的键都不会为空。
  • s 中不会有嵌套括号对。
  • keyi 和 valuei 只包含小写英文字母。
  • knowledge 中的 keyi 不会重复。
@yankewei yankewei added the 中等 题目难度为中等 label Mar 28, 2021
@yankewei
Copy link
Owner Author

yankewei commented Mar 28, 2021

正则

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 "?"
	}
    })
}

@yankewei
Copy link
Owner Author

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()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
中等 题目难度为中等
Projects
None yet
Development

No branches or pull requests

1 participant