网站seo分析报告,唐山小程序开发公司,263企业邮箱手机版登录,浙江江能建设有限公司网站文章目录1. 题目2. 解题1. 题目
某个字符串 S 需要执行一些替换操作#xff0c;用新的字母组替换原有的字母组#xff08;不一定大小相同#xff09;。
每个替换操作具有 3 个参数#xff1a;起始索引 i#xff0c;源字 x 和目标字 y。
规则是#xff1a;如果 x 从原始…
文章目录1. 题目2. 解题1. 题目
某个字符串 S 需要执行一些替换操作用新的字母组替换原有的字母组不一定大小相同。
每个替换操作具有 3 个参数起始索引 i源字 x 和目标字 y。
规则是如果 x 从原始字符串 S 中的位置 i 开始那么就用 y 替换出现的 x。如果没有则什么都不做。
举个例子如果 S “abcd” 并且替换操作 i 2x “cd”y “ffff”那么因为 “cd” 从原始字符串 S 中的位置 2 开始所以用 “ffff” 替换它。
再来看 S “abcd” 上的另一个例子如果一个替换操作 i 0x “ab”y “eee”以及另一个替换操作 i 2x “ec”y “ffff”那么第二个操作将不会执行因为原始字符串中 S[2] c与 x[0] e 不匹配。
所有这些操作同时发生。保证在替换时不会有任何重叠 S abc, indexes [0, 1], sources [ab,bc] 不是有效的测试用例。
示例 1
输入S abcd, indexes [0,2], sources [a,cd],
targets [eee,ffff]
输出eeebffff
解释
a 从 S 中的索引 0 开始所以它被替换为 eee。
cd 从 S 中的索引 2 开始所以它被替换为 ffff。示例 2
输入S abcd, indexes [0,2], sources [ab,ec],
targets [eee,ffff]
输出eeecd
解释
ab 从 S 中的索引 0 开始所以它被替换为 eee。
ec 没有从原始的 S 中的索引 2 开始所以它没有被替换。提示
0 S.length 1000
S 仅由小写英文字母组成
0 indexes.length 100
0 indexes[i] S.length
sources.length indexes.length
targets.length indexes.length
1 sources[i].length, targets[i].length 50
sources[i] 和 targets[i] 仅由小写英文字母组成来源力扣LeetCode 链接https://leetcode-cn.com/problems/find-and-replace-in-string 著作权归领扣网络所有。商业转载请联系官方授权非商业转载请注明出处。 2. 解题
字符串替换 http://www.cplusplus.com/reference/string/string/replace/
class Solution {
public:string findReplaceString(string S, vectorint indexes, vectorstring sources, vectorstring targets) {int n S.size(), i 0, k, len;unordered_mapint,int m;for(i 0; i indexes.size(); i) {m[indexes[i]] i;//原始数值 -- 对应的原始序号}sort(indexes.begin(), indexes.end());for(i int(indexes.size())-1; i 0; i--){ //从大的序号开始替换不需考虑序号变化k m[indexes[i]];len sources[k].size();if(S.substr(indexes[i], len) sources[k]){S.replace(indexes[i], len, targets[k]);}}return S;}
};8 ms 10.7 MB 我的CSDN博客地址 https://michael.blog.csdn.net/
长按或扫码关注我的公众号Michael阿明一起加油、一起学习进步