Leetcode每日一题:833. 字符串中的查找与替换(2023.8.15 C++)

目录

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

题目描述:

实现代码与思路:

哈希表 + 模拟

原理思路:


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

题目描述:

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

要完成第 i 个替换操作:

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

例如,如果 s = "abcd" , indices[i] = 0 , sources[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 开始,所以它没有被替换。

实现代码与思路:

哈希表 + 模拟

若一个位置有多个匹配

class Solution {
public:
    string findReplaceString(string s, vector<int>& indices, vector<string>& sources, vector<string>& targets) {

        unordered_map<int, vector<int>> m;
        string res = "";

        for (int i = 0; i < indices.size(); i++)
            m[indices[i]].push_back(i);
        
        // 遍历
        for (int i = 0; i < s.size();)
        {
            int flag = 0;
            if (m.count(i)) // 存在从这里开始的替换
            {
                for (auto t: m[i]) // 一个位置可能有多个替换
                {
                    // 相同
                    if (s.substr(i, sources[t].size()) == sources[t])
                    {
                        flag = 1;
                        res += targets[t];
                        i += sources[t].size(); // 移动
                        break;
                    }
                }
            }
            if (flag == 0)
            {
                res += s[i];
                i++;
            }
        }
        return res;
    }
}; 

不过此题说了不重复,可以直接这样写:

class Solution {
public:
    string findReplaceString(string s, vector<int>& indices, vector<string>& sources, vector<string>& targets) {

        unordered_map<int, int> m;
        string res = "";

        for (int i = 0; i < indices.size(); i++)
            m[indices[i]] = i;

        // 遍历 
        for (int i = 0; i < s.size();)
        {
            if (m.count(i)) // 存在从这里开始的替换
            {
                int t = m[i];
                if (s.substr(i, sources[t].size()) == sources[t])
                {
                    res += targets[t]; // 替换
                    i += sources[t].size(); // 移动
                    continue;
                }
            }
            res += s[i];
            i++;

        }
        return res;
    }
};

原理思路:

        map储存idx位置和sources,target的下下标,然后模拟即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cosmoshhhyyy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值