Find And Replace in String

To some string S, we will perform some replacement operations that replace groups of letters with new ones (not necessarily the same size).

Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y.  The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y.  If not, we do nothing.

For example, if we have S = "abcd" and we have some replacement operation i = 2, x = "cd", y = "ffff", then because "cd" starts at position 2 in the original string S, we will replace it with "ffff".

Using another example on S = "abcd", if we have both the replacement operation i = 0, x = "ab", y = "eee", as well as another replacement operation i = 2, x = "ec", y = "ffff", this second operation does nothing because in the original string S[2] = 'c', which doesn't match x[0] = 'e'.

All these operations occur simultaneously.  It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0, 1], sources = ["ab","bc"] is not a valid test case.

Example 1:

Input: S = "abcd", indexes = [0,2], sources = ["a","cd"], targets = ["eee","ffff"]
Output: "eeebffff"
Explanation: "a" starts at index 0 in S, so it's replaced by "eee".
"cd" starts at index 2 in S, so it's replaced by "ffff".

Example 2:

Input: S = "abcd", indexes = [0,2], sources = ["ab","ec"], targets = ["eee","ffff"]
Output: "eeecd"
Explanation: "ab" starts at index 0 in S, so it's replaced by "eee".
"ec" doesn't starts at index 2 in the original S, so we do nothing.

Notes:

  1. 0 <= indexes.length = sources.length = targets.length <= 100
  2. 0 < indexes[i] < S.length <= 1000
  3. All characters in given inputs are lowercase letters.
 1 class Solution {
2 public:
3 string findReplaceString(string S, vector<int>& indexes, vector<string>& sources, vector<string>& targets) {
4 string res;
5 res = S;
6 map<int,int> mp; // indexes's value relate to index after sorting
7 for(int i = 0; i < indexes.size();i++){
8 mp[indexes[i]] = i;
9 }
10 vector<int> cpi = indexes;
11 sort(cpi.begin(),cpi.end()); //cpi.end() means the pos after the last number
12
13 for(int i = cpi.size()-1; i>=0 ;i--){ //from big to small
14 int n = cpi[i];
15 int m = mp[cpi[i]];
16 if(sources[m] == S.substr(n,sources[m].size())){ //substr(int pos, int len)
17 res.replace(n,sources[m].size(),targets[m]); //replace(int pos, int len, string str) or ::iterator
18 }
19 }
20 return res;
21 }
22 };

主要操作是取子串和替代的操作。

主要算法是sort和从大到小操作以避免index改变造成的不能实现同时替换的问题。

833. Find And Replace in String —— weekly contest 84的更多相关文章

  1. 【LeetCode】833. Find And Replace in String 解题报告(Python)

    [LeetCode]833. Find And Replace in String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...

  2. LC 833. Find And Replace in String

    To some string S, we will perform some replacement operations that replace groups of letters with ne ...

  3. 833. Find And Replace in String

    To some string S, we will perform some replacement operations that replace groups of letters with ne ...

  4. 835. Image Overlap —— weekly contest 84

    Image Overlap Two images A and B are given, represented as binary, square matrices of the same size. ...

  5. 834. Sum of Distances in Tree —— weekly contest 84

    Sum of Distances in Tree An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges a ...

  6. 832. Flipping an Image —— weekly contest 84

    Flipping an Image Given a binary matrix A, we want to flip the image horizontally, then invert it, a ...

  7. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  8. Leetcode Weekly Contest 86

    Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...

  9. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

随机推荐

  1. osgEarth使用笔记4——加载矢量数据

    目录 1. 概述 2. 详论 2.1. 基本绘制 2.2. 矢量符号化 2.2.1. 可见性 2.2.2. 高度设置 2.2.3. 符号化 2.2.4. 显示标注 2.3. 其他 3. 结果 4. 问 ...

  2. Linux 的shell指令

    ------十六进制查看文件 ------  ls 命令 ---- cp  ----- chmod

  3. Mice and Rice(queue的用法)

    Mice and Rice(queue的用法) Mice and Rice is the name of a programming contest in which each programmer ...

  4. 《C++primerplus》第12章“队列模拟”程序

    这个程序刚开始学有很多难点,个人认为主要有以下三项: 1.链表的概念 2.如何表示顾客随机到达的过程 3.程序执行时两类之间的关系,即执行逻辑 关于第一点,书上的图解释得比较清楚了,把"空指 ...

  5. A4988两相四线步进电机驱动模块使用经验

    1.A4988模块可以驱动两相四线步进电机,模块引脚及接线图如下: 2.步进电机引线如下: 3.引脚: ENABLE:低电平有效,用于打开和关闭场效应管的输出: RESET:低电平有效,芯片复位: S ...

  6. 第一次使用HSDB

    今天看了几篇大佬关于HSDB使用的文章,自己也依样画葫芦的用来一下,强大的一匹!!! HSDB(Hotspot Debugger),JDK自带的工具,用于查看JVM运行时的状态. HSDB位于C:\P ...

  7. linux查看进程内存使用情况,以及将线程情况输出文件

    用jmap把进程内存使用情况dump到文件中,再用jhat分析查看.jmap进行dump命令格式如下: jmap -dump:format=b,file=/tmp/dump.dat 21711  -- ...

  8. 从0到1进行Spark history分析

    一.总体思路 以上是我在平时工作中分析spark程序报错以及性能问题时的一般步骤.当然,首先说明一下,以上分析步骤是基于企业级大数据平台,该平台会抹平很多开发难度,比如会有调度日志(spark-sub ...

  9. Hudi on Flink在顺丰的实践应用

    ​ 获取PDF版本 请关注"实时流式计算" 后台回复 "flink1015"

  10. TiOps,支持容器,支持多云安全远程运维,疫情期间免费开放,助力远程办公

    TiOps,支持多云环境安全远程运维,疫情期间免费对外开放在疫情期间,为减少疾病传染可能性,许多公司的选择了在家远程办公.对于运维来说,既要远程运维,又要保证安全,还要在复杂的IT环境中保持高效,面临 ...