• LeetCode每日一题(1807. Evaluate the Bracket Pairs of a String)


    You are given a string s that contains some bracket pairs, with each pair containing a non-empty key.

    For example, in the string “(name)is(age)yearsold”, there are two bracket pairs that contain the keys “name” and “age”.
    You know the values of a wide range of keys. This is represented by a 2D string array knowledge where each knowledge[i] = [keyi, valuei] indicates that key keyi has a value of valuei.

    You are tasked to evaluate all of the bracket pairs. When you evaluate a bracket pair that contains some key keyi, you will:

    Replace keyi and the bracket pair with the key’s corresponding valuei.
    If you do not know the value of the key, you will replace keyi and the bracket pair with a question mark “?” (without the quotation marks).
    Each key will appear at most once in your knowledge. There will not be any nested brackets in s.

    Return the resulting string after evaluating all of the bracket pairs.

    Example 1:

    Input: s = “(name)is(age)yearsold”, knowledge = [[“name”,“bob”],[“age”,“two”]]
    Output: “bobistwoyearsold”

    Explanation:
    The key “name” has a value of “bob”, so replace “(name)” with “bob”.
    The key “age” has a value of “two”, so replace “(age)” with “two”.

    Example 2:

    Input: s = “hi(name)”, knowledge = [[“a”,“b”]]
    Output: “hi?”

    Explanation: As you do not know the value of the key “name”, replace “(name)” with “?”.

    Example 3:

    Input: s = “(a)(a)(a)aaa”, knowledge = [[“a”,“yes”]]
    Output: “yesyesyesaaa”

    Explanation: The same key can appear multiple times.
    The key “a” has a value of “yes”, so replace all occurrences of “(a)” with “yes”.
    Notice that the "a"s not in a bracket pair are not evaluated.

    Constraints:

    • 1 <= s.length <= 105
    • 0 <= knowledge.length <= 105
    • knowledge[i].length == 2
    • 1 <= keyi.length, valuei.length <= 10
    • s consists of lowercase English letters and round brackets ‘(’ and ‘)’.
    • Every open bracket ‘(’ in s will have a corresponding close bracket ‘)’.
    • The key in each bracket pair of s will be non-empty.
    • There will not be any nested bracket pairs in s.
    • keyi and valuei consist of lowercase English letters.
    • Each keyi in knowledge is unique.

    建立两个 buffer, 一个保存括号中的 key, 一个保存答案, 遇到’(‘后,往后的所有非’)‘字符都放到 key buffer 中, 遇到’)'后将 key buffer 中的字符串拿出来查是否有对应的 value, 如果有就往答案中 push value,如果没有则往答案中 push ‘?’, 不管是那种情况, 最后都要清理 key buffer


    
    use std::collections::HashMap;
    
    impl Solution {
        pub fn evaluate(s: String, knowledge: Vec<Vec<String>>) -> String {
            let knowledge = knowledge
                .into_iter()
                .map(|l| (l[0].clone(), l[1].clone()))
                .collect::<HashMap<String, String>>();
            let mut buff = String::new();
            let mut in_bracket = false;
            let mut ans = String::new();
            for c in s.chars() {
                match c {
                    '(' => {
                        in_bracket = true;
                    }
                    ')' => {
                        if let Some(v) = knowledge.get(&buff) {
                            ans.push_str(v);
                        } else {
                            ans.push('?');
                        }
                        buff.clear();
                        in_bracket = false;
                    }
                    _ => {
                        if in_bracket {
                            buff.push(c);
                            continue;
                        }
                        ans.push(c);
                    }
                }
            }
            ans
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
  • 相关阅读:
    企业微信的自动下班是什么意思?
    《Seq2Path: Generating Sentiment Tuples as Paths of a Tree》论文阅读
    网址在线封装APK系统源码
    自动化测试框架有哪几种?全网最全面的总结来了
    缓冲区与C库函数的实现
    中台框架模块开发实践-代码生成器的添加及使用
    第四章-保护模式
    【Java 基础篇】Java网络编程实战:P2P文件共享详解
    GSN前瞻预处理
    python项目-飞机大战
  • 原文地址:https://blog.csdn.net/wangjun861205/article/details/126094531