当前位置:网站首页>LeetCode每日一题(1807. Evaluate the Bracket Pairs of a String)
LeetCode每日一题(1807. Evaluate the Bracket Pairs of a String)
2022-08-01 20:41:00 【wangjun861205】
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
}
}
边栏推荐
- Redis does check-in statistics
- 实用新型专利和发明专利的区别?秒懂!
- 【节能学院】安科瑞餐饮油烟监测云平台助力大气污染攻坚战
- 98. Embedded controller EC actual combat EC development board development completed
- Buttons with good user experience should not have hover state on mobile phones
- 宝塔搭建PESCMS-Ticket开源客服工单系统源码实测
- 面试突击70:什么是粘包和半包?怎么解决?
- 多线程之生产者与消费者
- 【luogu P1912】诗人小G(二分栈)(决策单调性优化DP)
- tiup mirror clone
猜你喜欢

To promote energy conservation institute 】 【 the opinions of the agricultural water price reform

WhatsApp群发实战分享——WhatsApp Business API账号

Based on FPGA in any number of bytes (single-byte or multibyte) serial port (UART) to send (including source engineering)

Digital twin Beijing the imperial palace, yuan universe is the process of tourism

Pytorch框架学习记录9——非线性激活

StringTable Detailed String Pool Performance Tuning String Concatenation

【nn.Parameter()】生成和为什么要初始化

MySQL语法基础

用户身份标识与账号体系实践
![[Personal work] Wireless network image transmission module](/img/64/c0cec74692df7ca05c1a5317e21c9d.png)
[Personal work] Wireless network image transmission module
随机推荐
Pytorch框架学习记录8——最大池化的使用
任务调度线程池基本介绍
二维、三维、四维矩阵每个维度含义解释
Multithreaded producers and consumers
启明云端分享|盘点ESP8684开发板有哪些功能
【Untitled】
"No title"
外骨骼机器人(七):标准步态数据库
SIPp 安装及使用
Hangao data import
Redis 做网页UV统计
StringTable详解 串池 性能调优 字符串拼接
Batch get protein .pdb files based on Uniprot ID/PDB ID
作为程序员你应该会的软件
tiup mirror init
[Personal work] Wireless network image transmission module
[Multi-task model] Progressive Layered Extraction: A Novel Multi-Task Learning Model for Personalized (RecSys'20)
OSG Notes: Set DO_NOT_COMPUTE_NEAR_FAR to manually calculate far and near planes
Which websites are commonly used for patent searches?
多线程之生产者与消费者