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. 用 Portainer 远程管理 docker

    参考的官网地址为:https://portainer.readthedocs.io/en/stable/deployment.html 先更新Centos docker 镜像加速地址: curl -s ...

  2. Winfrom UI 美化 MetroModernUI库应用实例

    使用方式: 选择项目==>右键==>管理NuGet安装包==>输入Metro==> ==>添加选项卡(自定义命名,例如Metrol UI)==>浏览 ==>加 ...

  3. 记录一下Web开发环境搭建 Eclipse-Java EE 篇

    转自https://www.cnblogs.com/yangyxd/articles/5615965.html Web开发环境搭建 Eclipse-Java EE 篇 [原创内容,转载注名出处] 1. ...

  4. Python:面向对象编程2

    types.MethodType __slot__ @property,  @xxx.setter Python的多重继承和MinIn 如何在class创建后,给实例绑定属性和方法? (动态绑定/定义 ...

  5. 浅谈浏览器存储(cookie、localStorage、sessionStorage)

    今天我们从前端的角度了解一下浏览器存储,我们常见且常用的存储方式主要由两种:cookie.webStorage(localStorage和sessionStorage).下面我们来一一认识它们. Co ...

  6. linux内核 同步

    锁 linux本身实现了集中锁机制,各种锁机制之间的差别是,当锁已经被其他线程持有的时候,一些锁使用自旋的方式来等待,另外一些锁会当当前线程改变为睡眠状态然后等待被唤醒. 锁的争用 如果一个锁处于高度 ...

  7. 使用OmniDiskSweeper清理MAC

    Mac 经常提示我磁盘空间已满,管理磁盘空间. 然后我就管理了一下,发现系统竟占90个G,有点懵逼.然后网上查了资料 使用了一个名叫OmniDiskSweeper的超级强大的工具,而且还是免费的,它能 ...

  8. Python的f.seek(offset, whence)函数

    file.seek()方法标准格式是:seek(offset,whence=0)offset:开始的偏移量,也就是代表需要移动偏移的字节数whence:给offset参数一个定义,表示要从哪个位置开始 ...

  9. VUE: 移动端长按弹出确认删除地址(后面测试发现IOS有BUG,后面有更新随笔,更新后的亲测有效)

    收货地址的删除方式可能有很多种,我目前见过的暂时只有两种(1.在编辑页删除  2.长按某一条收货地址弹出是否删除地址) 在开发的项目上要求第二种删除方法,于是记录一下我写的代码 ~ 1.首先,在移动端 ...

  10. nodejs(上)(获取请求参数)

    Node.js是一个让JavaScript运行在服务器端的开发平台    参考文章 nodejs特点: 单线程 异步非阻塞i/o(异步相对节省资源,把那个等待的时间利用上了) 事件驱动 稳定性差(因为 ...