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.

简单的Median题,用一个index根据index的起始点和长度在原字符串中匹配。注意index可能无序,要先排序以后再用。

Runtime: 4 ms, faster than 99.79% of C++ online submissions for Find And Replace in String.

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#include <vector>
#include <iostream>
#include <unordered_map>
#include <map>
using namespace std;
class Solution {
private:
map<int, vector<string>> mp;
public:
string findReplaceString(string S, vector<int>& indexes, vector<string>& sources, vector<string>& targets) {
REP(i, indexes.size()){
mp[indexes[i]].push_back(sources[i]);
mp[indexes[i]].push_back(targets[i]);
}
int cnt = ;
for(auto it : mp){
indexes[cnt] = it.first;
sources[cnt] = it.second[];
targets[cnt] = it.second[];
cnt++;
}
vector<string> strvec;
string ret;
int idx = ;
REP(i,indexes.size()){
if(idx < indexes[i]){
strvec.push_back(S.substr(idx,indexes[i] - idx));
idx = indexes[i];
}
if(S.substr(indexes[i], sources[i].size()) == sources[i]){
strvec.push_back(targets[i]);
idx += sources[i].size();
}
}
if(idx < S.size()){
strvec.push_back(S.substr(idx));
}
for(auto s : strvec){
ret += s;
}
return ret;
}
};

LC 833. Find And Replace in String的更多相关文章

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

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

  2. 833. Find And Replace in String —— weekly contest 84

    Find And Replace in String To some string S, we will perform some replacement operations that replac ...

  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. String.replace与String.format

    字符串的替换函数replace平常使用的频率非常高,format函数通常用来填补占位符.下面简单总结一下这两个函数的用法. 一.String.replace的两种用法 replace的用法如:repl ...

  5. [转]String.Replace 和 String.ReplaceAll 的区别

    JAVA 中的 replace replaceAll 问题: 测试code System.out.println("1234567890abcdef -----> "+&qu ...

  6. Java: Replace a string from multiple replaced strings to multiple substitutes

    Provide helper methods to replace a string from multiple replaced strings to multiple substitutes im ...

  7. [LeetCode] Find And Replace in String 在字符串中查找和替换

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

  8. [Swift]LeetCode833. 字符串中的查找与替换 | Find And Replace in String

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

  9. JAVA中string.replace()和string.replaceAll()的区别及用法

    乍一看,字面上理解好像replace只替换第一个出现的字符(受javascript的影响),replaceall替换所有的字符,其实大不然,只是替换的用途不一样.    public String r ...

随机推荐

  1. JavaMaven【七、插件使用】

    配置pom.xml 配置在那个周期的那个阶段执行该插件的功能 上图是配置了使用插件source3.0.1,该插件的功能是打包源码 并配置了在package阶段后执行打包源码的操作jar-no-fork ...

  2. js数组的所有方法

    修改器方法 下面的这些方法会改变调用它们的对象自身的值: Array.prototype.copyWithin()  在数组内部,将一段元素序列拷贝到另一段元素序列上,覆盖原有的值. Array.pr ...

  3. Creating a PXE Configuration File

      The PXE configuration file defines the menu displayed to the pxe client host as it boots up and co ...

  4. python中的负数取模问题(一个大坑)

    先来看一段代码 这是什么情况?为什么会出现这种结果.我们再来看看其它语言的执行结果 我们用golang.js.c分别算了一下,结果得到的结果都是一致的,但是python为啥不一样呢? 其实之所以这么做 ...

  5. python网络编程:TCP通讯模板、粘包及解决方案、自定义报头

    一.TCP通讯模板 二.远程CMD程序 三.解决粘包问题 四.解决粘包问题2 一.TCP通讯模板 TCP客户端 import socket c = socket.socket() # 连接服务器 c. ...

  6. 3.Https服务器的配置

    1.前言: 所谓区块链,简而言之就是一种数据结构,每一个区块都像账本的每一页纸记录了该网络上的交易信息,而众多区块在时间的基础上按照顺序连接起 来就形成了区块链.区块链能够以数字方式识别和跟踪交易,并 ...

  7. Redis02——Redis单节点安装

    Redis单节点安装 一.Redis的数据类型 string hash list set zset 二.安装 2.1.下载 wget http://download.redis.io/releases ...

  8. js实现购物车数量的增加与减少,js实现购物车数量的自增与自减

    js实现购物车数量的增加与减少,js实现购物车数量的自增与自减 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//E ...

  9. Android异常与性能优化相关面试问题-内存泄漏面试问题讲解

    Java内存泄漏基础知识: Java的内存的分配策略 a.静态存储区:也叫方法区,主要是存放一些静态数据及全局变量等,在程序编译时就已经分配好了,并且在静态存储区中存放的变量在整个程序运行期间都存在. ...

  10. libusb_control_setup

     libusb_fill_control_transfer(transfer, devh, buf, ctrl_urb_complete_cb,             utrans, 1000); ...