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. Mysql中 int(3) 类型的含义

    注意:这里的(3)代表的并不是存储在数据库中的具体的长度,以前总是会误以为int(3)只能存储3个长度的数字,int(11)就会存储11个长度的数字,这是大错特错的. 其实当我们在选择使用int的类型 ...

  2. 063 01 Android 零基础入门 01 Java基础语法 08 Java方法 01 无参无返回值方法

    063 01 Android 零基础入门 01 Java基础语法 08 Java方法 01 无参无返回值方法 本文知识点:无参无返回值方法 无参无返回值方法 案例 为什么使用方法?--方便复杂问题调用 ...

  3. 批处理文件的@echo off

    转载:https://blog.csdn.net/zl1zl2zl3/article/details/79218448  @echo off  关闭回显     @echo on  打开回显      ...

  4. 关于freemodbus协议中eMBFuncReadHoldingRegister()函数的所谓错误

    摘要:网上看到有好心的网友提示,freemodbus协议中的mbfuncholding.c 文件中eMBFuncReadHoldingRegister()函数,有一处错误,即:第185行的" ...

  5. STM32之旅2——按键

    STM32之旅2--按键     几乎每个项目都有用到按键,为了避免以后在做大项目的时候还在琢磨按键怎么写,现在写一个,方便以后使用.这里是最简单的独立按键驱动方法,和学习51单片机是的一样,更好的方 ...

  6. protoc-c 安装记录

    记录下 protobuf-c 安装过程中的问题. 1) 安装的时候没细看依赖. --  protobuf-c requires a C compiler, a C++ compiler, protob ...

  7. 用python you-get下载视频

    安装python3后 安装you-get包: pip3 install you-get 下载视频: 打开windows终端:运行 you-get url 查看视频信息: you-get -i url ...

  8. vim插件配置

    OS:kali linux tool:vim 上图: 0x00 需要用到的插件及其下载地址 左边的一栏显示文件目录结构的用到的插件为 NERDTree 下载地址:https://github.com/ ...

  9. Vue.js 学习笔记之五:编译 vue 组件

    正如上一篇笔记中所说,直接使用 ES6 标准提供的模块规范来编写 Vue 组件在很多情况下可能并不是最佳实践.主要原因有两个,首先是市面上还有许多并没有对 ES6 标准提供完全支持的 Web 浏览器, ...

  10. docker的常用操作之三:网络配置

    一, docker安装后容器使用哪些网络类型? 在宿主机执行如下命令: [root@localhost liuhongdi]# docker network ls NETWORK ID NAME DR ...