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

LeetCode 0833. 字符串中的查找与替换

【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.lryc.cn/news/127012.html

相关文章:

  • Redis对象和五种常用数据类型
  • 常用的Elasticsearch查询DSL
  • 计算机网络笔记
  • 高效反编译luac文件
  • 密码湘军,融合创新!麒麟信安参展2023商用密码大会,铸牢数据安全坚固堡垒
  • 关于视频监控平台EasyCVR视频汇聚平台建设“明厨亮灶”具体实施方案以及应用
  • 区块链系统探索之路:私钥的压缩和WIF格式详解
  • DiffusionDet: Diffusion Model for Object Detection
  • CH01_重构、第一个示例
  • 学习篇之React Fiber概念及原理
  • 商城-学习整理-高级-全文检索-ES(九)
  • 无人机跟随一维高度避障场景--逻辑分析
  • Android Studio Giraffe控制台乱码
  • 云原生 envoy xDS 动态配置 java控制平面开发 支持restful grpc实现 EDS 动态endpoint配置
  • Linux--实用指令与方法(部分)
  • 常见期权策略类型有哪些?
  • tomcat服务七层搭建动态页面查看
  • sql A表(含有部分B表字段) 向B表插入A表数据
  • 如何用思维导图+Markdown提升工作效率?
  • 睿趣科技:抖音开网店现在做还来得及吗
  • C++——list的简要介绍
  • Java自学网站推荐,专业教学快速提升
  • 深入学习SpringCloud Alibaba微服务架构,揭秘Nacos、Sentinel、Seata等核心技术,助力构建高效系统!
  • 【iMessage频發软件苹果群发技术开源原创】当 APNs 发送通知到一个离线设备时,APNs 会把通知存储起来(一定的时间内),当设备上线时再递送给设备。
  • 【数据结构】_8.二叉树OJ
  • 酷开系统 | 酷开科技大数据,更好的与目标消费人群建立联系
  • 无涯教程-Perl - study函数
  • dfs深度搜索入门之滑雪
  • Python程序设计——元组、集合和字典
  • 八股文之框架篇(Spring Boot、SSM)