LeetCode解法汇总833. 字符串中的查找与替换

目录链接:

力扣编程题-解法汇总_分享+记录-CSDN博客

GitHub同步刷题项目:

https://github.com/September26/java-algorithms

原题链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

描述:

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

要完成第 i 个替换操作:

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

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

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

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

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

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

示例 1:

复制代码
输入:s = "abcd", indices = [0,2], sources = ["a","cd"], targets = ["eee","ffff"]
输出:"eeebffff"
解释:
"a" 从 s 中的索引 0 开始,所以它被替换为 "eee"。
"cd" 从 s 中的索引 2 开始,所以它被替换为 "ffff"。

示例 2:

复制代码
输入:s = "abcd", indices = [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 <= indices[i] < s.length
  • 1 <= sources[i].length, targets[i].length <= 50
  • s 仅由小写英文字母组成
  • sources[i]targets[i] 仅由小写英文字母组成

解题思路:

* 833. 字符串中的查找与替换

* 解题思路:

* 首先,把indices,sources,targets融合成一个数组list,按照indices的大小排序。

* 然后遍历这个,尝试使用list中indices的值去查找,看对应的位置是否匹配,如果匹配,则需要插入两部分:

* 1.indice之前的部分。

* 2.替换为targets的部分。

* 然后更新index为当前位置即可。

* 如果不匹配,则无需更新,因为下次的成功匹配会填充index到当前匹配位置所有的值。

代码:

复制代码
class Solution833
{
public:
    string findReplaceString(string s, vector<int> &indices, vector<string> &sources, vector<string> &targets)
    {
        vector<pair<int, pair<string, string>>> list;
        for (int i = 0; i < indices.size(); i++)
        {
            list.push_back(make_pair(indices[i], make_pair(sources[i], targets[i])));
        }
        sort(list.begin(), list.end(), [](pair<int, pair<string, string>> pair1, pair<int, pair<string, string>> pair2)
             { return pair2.first - pair1.first > 0; });

        string out;
        int index = 0;
        for (auto item : list)
        {
            // 如果相同,则填入替换的
            string olds = s.substr(item.first, min(item.second.first.size(), s.size() - item.first));
            if (olds == item.second.first)
            {
                out.append(s.substr(index, item.first - index));
                out.append(item.second.second);
                index = item.first + item.second.first.size();
            }
        }
        out.append(s.substr(index, s.size() - index));
        return out;
    }
};
相关推荐
政企项目老覃10 小时前
大模型幻觉治理与自动评测:金融风控场景的落地实践
人工智能·算法·机器学习
淡海水11 小时前
08-03-不可变-ImmutableDictionary-TKey-TValue-与ImmutableHashSet-T-持久化哈希树
数据结构·算法·c#·哈希算法·dictionary·immutable
hansang_IR11 小时前
【题解】[APIO2023] 赛博乐园 / cyberland
c++·算法·图论
洛阳纸贵11 小时前
MATLAB-matlab基础知识
学习·算法·matlab
落羽的落羽11 小时前
【AI】快速理解AI应用的相关名词概念
linux·c++·人工智能·python·计算机网络·算法
Nil20812 小时前
leetcode 17电话号码的字母组合
算法·leetcode·职场和发展
203号居民13 小时前
LeetCode hot 100 — 25. K 个一组翻转链表
算法·leetcode·链表
ocean210314 小时前
2025-2026年AI算法与模型研发面试高频知识点洞察
人工智能·算法·面试
Nil20814 小时前
leetcode 78子集
数据结构·算法·leetcode