当前位置: 首页 > news >正文

110平米三室一厅简装windows优化大师手机版

110平米三室一厅简装,windows优化大师手机版,短视频素材免费下载网站,wordpress中文附件【LetMeFly】833.字符串中的查找与替换 力扣题目链接:https://leetcode.cn/problems/find-and-replace-in-string/ 你会得到一个字符串 s (索引从 0 开始),你必须对它执行 k 个替换操作。替换操作以三个长度均为 k 的并行数组给出:indices,…

【LetMeFly】833.字符串中的查找与替换

力扣题目链接:https://leetcode.cn/problems/find-and-replace-in-string/

你会得到一个字符串 s (索引从 0 开始),你必须对它执行 k 个替换操作。替换操作以三个长度均为 k 的并行数组给出:indicessources,  targets

要完成第 i 个替换操作:

  1. 检查 子字符串  sources[i] 是否出现在 原字符串 s 的索引 indices[i] 处。
  2. 如果没有出现, 什么也不做 。
  3. 如果出现,则用 targets[i] 替换 该子字符串。

例如,如果 s = "abcd" , indices[i] = 0sources[i] = "ab"targets[i] = "eee" ,那么替换的结果将是 "eeecd"

所有替换操作必须 同时 发生,这意味着替换操作不应该影响彼此的索引。测试用例保证元素间不会重叠

  • 例如,一个 s = "abc" ,  indices = [0,1]sources = ["ab","bc"] 的测试用例将不会生成,因为 "ab""bc" 替换重叠。

在对 s 执行所有替换操作后返回 结果字符串

子字符串 是字符串中连续的字符序列。

 

示例 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 开始,所以它没有被替换。

 

提示:

  • 1 <= s.length <= 1000
  • k == indices.length == sources.length == targets.length
  • 1 <= k <= 100
  • 0 <= indexes[i] < s.length
  • 1 <= sources[i].length, targets[i].length <= 50
  • s 仅由小写英文字母组成
  • sources[i]targets[i] 仅由小写英文字母组成

方法一:模拟

首先将“替换信息”indicessourcestargets打包起来,按照indices从小到大排序,记为v

写一个函数equal(s, toCmp, start)用来判断sstart处开始是否与toCmp匹配。

这样,我们只需要用下标 i i i遍历s

  • i i i等于 v v v中待处理的 i n d i c e s indices indices,看字符串 s s s i i i处开始是否与 v v v中待处理的 s o u r c e s sources sources匹配:

    • 若匹配:进行替换(答案加上对应的 t a r g e t s targets targets i i i加上被替换掉的字符串的长度减1)
    • 否则:不进行替换(答案加上 s [ i ] s[i] s[i]
  • 否则:不进行替换(答案加上 s [ i ] s[i] s[i]

  • 时间复杂度 O ( C + n log ⁡ n ) O(C + n\log n) O(C+nlogn),其中 C C C s o u r c e s sources sources t a r g e t s targets targets中字母个数之和, n = l e n ( s o u r c e s ) n=len(sources) n=len(sources)

  • 空间复杂度 O ( C + log ⁡ n ) O(C + \log n) O(C+logn)

AC代码

C++

class Solution {
private:bool equal(string& s, string& toCmp, int start) {  // 返回s从下标start开始,是否与toCmp匹配if (start + toCmp.size() > s.size()) {return false;}for (int i = 0; i < toCmp.size(); i++) {if (s[start + i] != toCmp[i]) {return false;}}return true;}public:string findReplaceString(string& s, vector<int>& indices, vector<string>& sources, vector<string>& targets) {vector<tuple<int, string, string>> v;for (int i = 0; i < indices.size(); i++) {v.push_back({indices[i], sources[i], targets[i]});}sort(v.begin(), v.end(), [](tuple<int, string, string>& a, tuple<int, string, string>& b) {return get<0>(a) < get<0>(b);});string ans;int nowV = 0;for (int i = 0; i < s.size(); i++) {if (nowV < v.size() && get<0>(v[nowV]) == i) {if (equal(s, get<1>(v[nowV]), i)) {ans += get<2>(v[nowV]);i += get<1>(v[nowV]).size() - 1;}else {ans += s[i];}nowV++;}else {ans += s[i];}}return ans;}
};

同步发文于CSDN,原创不易,转载经作者同意后请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/132289306

http://www.dinnco.com/news/38678.html

相关文章:

  • 柳州网站建设工作室sem和seo是什么意思
  • 网站开启gzip压缩引擎搜索网站
  • seo网站内容seo网络搜索引擎优化
  • 厦门创意网站建设专注于网站营销服务
  • 企业网站规划原则网络营销课程学什么
  • 网站seo排名查询南京网络优化培训
  • wordpress大型站点做专业搜索引擎优化
  • 钟祥网站建设淘宝指数在哪里查询
  • 升级的网站显示什么西安网约车
  • 免费网站怎么赚钱小学生抄写新闻20字
  • 在线制作图片及图片处理seo运营学校
  • 网站平台建设的作用友情链接代码模板
  • 网站建设的盈利模式小学生收集的新闻10条
  • 怎样凡科建设网站营销网站建设多少钱
  • 南京一对一网站建设深圳最新消息
  • 网站和新媒体建设审批制度成品视频直播软件推荐哪个好一点
  • 邯郸做wap网站建设cps广告联盟平台
  • 中英企业网站管理系统西安网络推广公司大全
  • 电商网站seo百度热搜关键词
  • 医院网站html模板国内最新消息新闻
  • 网站制作模板程序云南seo
  • 如何做网站性能优化常州免费网站建站模板
  • dedecms迁移wordpress惠州seo快速排名
  • 网站添加wordpress博客网推团队
  • webportal自助建站seo专员招聘
  • ECSHOP网站开发中心网络广告策划
  • 如何做网站网页免费b站好看的纪录片免费
  • 国外wordpress模板seo排名软件哪个好用
  • 郑州人才网站网站推广的作用在哪里
  • 商超网站网络营销课程思政