当前位置:网站首页>1807. 替换字符串中的括号内容
1807. 替换字符串中的括号内容
2022-07-04 21:23:00 【Mr Gao】
1807. 替换字符串中的括号内容
给你一个字符串 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” 不需要被替换。
这题显然用哈希表是比较简单的但是,作者这次使用的常规方法尝试的,感兴趣的可以学习一下:
int f(char *s,int index,char ***knowledge,int knowledgeSize){
int i;
for(i=0;i<knowledgeSize;i++){
char *p=knowledge[i][0];
int j=index,k=0,r=0;
while(s[j]!='('&&p[k]!='\0'){
if(s[j++]!=p[k++]){
r=1;
break;
}
}
if(r==0&&s[j]==')'&&p[k]=='\0'){
return i;
}
}
return -1;
}
char * evaluate(char * s, char *** knowledge, int knowledgeSize, int* knowledgeColSize){
char *re=(char *)malloc(sizeof(char )*(strlen(s)+2));
int resize=0;
int i=0;
while(s[i]!='\0'){
// printf("|i %d ",i);
if(s[i]!='('){
re[resize++]=s[i];
}
else{
int index=f(s,i+1,knowledge,knowledgeSize);
if(index==-1){
re[resize++]='?';
}
else{
char *p=knowledge[index][1];
int k=0;
while(p[k]!='\0'){
re[resize++]=p[k++];
}
}
while(s[i]!=')'){
i++;
}
}
i++;
}
re[resize]='\0';
return re;
}
边栏推荐
- [buuctf.reverse] 151_ [FlareOn6]DnsChess
- 案例分享|金融业数据运营运维一体化建设
- OMS系统实战的三两事
- minidom 模块写入和解析 XML
- How much is the minimum stock account opening commission? Is it safe to open an account online
- Operation of adding material schedule in SolidWorks drawing
- How to implement Devops with automatic tools
- [C language] deep understanding of symbols
- 股票开户流程是什么?使用同花顺手机炒股软件安全吗?
- MP3是如何诞生的?
猜你喜欢

Interpreting the development of various intelligent organizations in maker Education

从RepVgg到MobileOne,含mobileone的代码

TCP三次握手,四次挥手,你真的了解吗?

Redis has three methods for checking big keys, which are necessary for optimization

How to use concurrentlinkedqueue as a cache queue

GTEST from ignorance to proficiency (3) what are test suite and test case

如何借助自动化工具落地DevOps

Redis 排查大 key 的3种方法,优化必备

Flutter TextField示例

ArcGIS 10.2.2 | solution to the failure of ArcGIS license server to start
随机推荐
Use of redis publish subscription
GTEST from ignorance to proficiency (4) how to write unit tests with GTEST
VS2019 C# release下断点调试
How to implement Devops with automatic tools
旋变串判断
开源之夏专访|Apache IoTDB社区 新晋Committer谢其骏
Analyzing the maker space contained in steam Education
GTEST from ignorance to skillful use (1) GTEST installation
Jerry's ad series MIDI function description [chapter]
解读创客教育中的各类智能化组织发展
Flink1.13 SQL basic syntax (I) DDL, DML
Enlightenment of maker thinking in Higher Education
What is business intelligence (BI), just look at this article is enough
HDU - 1078 fatmouse and cheese (memory search DP)
Learning breakout 3 - about energy
Master the use of auto analyze in data warehouse
EhLib 数据库记录的下拉选择
Flutter 返回按钮的监听
Golang interview finishing three resumes how to write
el-tree结合el-table,树形添加修改操作