当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

How is the entered query SQL statement executed?

gtest从一无所知到熟练使用(3)什么是test suite和test case
![[ 每周译Go ] 《How to Code in Go》系列文章上线了!!](/img/bf/77253c87bfa1512f4b8d3d8f7ebe80.png)
[ 每周译Go ] 《How to Code in Go》系列文章上线了!!

Three or two things about the actual combat of OMS system
![[weekly translation go] how to code in go series articles are online!!](/img/bf/77253c87bfa1512f4b8d3d8f7ebe80.png)
[weekly translation go] how to code in go series articles are online!!

Exclusive interview of open source summer | new committer Xie Qijun of Apache iotdb community
![Jerry added the process of turning off the touch module before turning it off [chapter]](/img/28/5e4eb39243a0c973d0b90f76571f9b.png)
Jerry added the process of turning off the touch module before turning it off [chapter]

TCP协议三次握手过程
![[optimtool.unconstrained] unconstrained optimization toolbox](/img/ef/65379499df205c068ee9bc9df797ac.png)
[optimtool.unconstrained] unconstrained optimization toolbox

Redis 排查大 key 的3种方法,优化必备
随机推荐
并列图的画法,多排多列
HDU - 1078 fatmouse and cheese (memory search DP)
Delphi soap WebService server-side multiple soapdatamodules implement the same interface method, interface inheritance
Caduceus从未停止创新,去中心化边缘渲染技术让元宇宙不再遥远
[early knowledge of activities] list of recent activities of livevideostack
Lambdaquerywrapper usage
Learning breakout 3 - about energy
Jerry's ad series MIDI function description [chapter]
How is the entered query SQL statement executed?
flink1.13 sql基础语法(一)DDL、DML
GTEST from ignorance to skillful use (1) GTEST installation
巅峰不止,继续奋斗!城链科技数字峰会于重庆隆重举行
minidom 模塊寫入和解析 XML
Redis 排查大 key 的3种方法,优化必备
Cloudcompare & open3d DBSCAN clustering (non plug-in)
ArcGIS 10.2.2 | solution to the failure of ArcGIS license server to start
[ 每周译Go ] 《How to Code in Go》系列文章上线了!!
Jerry's ad series MIDI function description [chapter]
Rotary transformer string judgment
解析steam教育中蕴含的众创空间