LC 833. 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:
0 <= indexes.length = sources.length = targets.length <= 1000 < indexes[i] < S.length <= 1000- 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的更多相关文章
- 【LeetCode】833. Find And Replace in String 解题报告(Python)
[LeetCode]833. Find And Replace in String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...
- 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 ...
- 833. Find And Replace in String
To some string S, we will perform some replacement operations that replace groups of letters with ne ...
- String.replace与String.format
字符串的替换函数replace平常使用的频率非常高,format函数通常用来填补占位符.下面简单总结一下这两个函数的用法. 一.String.replace的两种用法 replace的用法如:repl ...
- [转]String.Replace 和 String.ReplaceAll 的区别
JAVA 中的 replace replaceAll 问题: 测试code System.out.println("1234567890abcdef -----> "+&qu ...
- 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 ...
- [LeetCode] Find And Replace in String 在字符串中查找和替换
To some string S, we will perform some replacement operations that replace groups of letters with ne ...
- [Swift]LeetCode833. 字符串中的查找与替换 | Find And Replace in String
To some string S, we will perform some replacement operations that replace groups of letters with ne ...
- JAVA中string.replace()和string.replaceAll()的区别及用法
乍一看,字面上理解好像replace只替换第一个出现的字符(受javascript的影响),replaceall替换所有的字符,其实大不然,只是替换的用途不一样. public String r ...
随机推荐
- ASP.NET实现验证码图片
新建一个checkcode.aspx文件,页面中不用写任何东西,在代码中,Page_Load中写入如下代码: string chkCode = string.Empty; int ix, ...
- linux的top下buffer与cache的区别、free命令内存解释
buffer: 缓冲区,一个用于存储速度不同步的设备或优先级不同的设备之间传输数据 的区域.通过缓冲区,可以使进程之间的相互等待变少,从而使从速度慢的设备读入数据 时,速度快的设备的操作进程不发 ...
- electron api sendInputEvent 源码
electron-master\electron-master\shell\browser\api\atom_api_web_contents.cc // Copyright (c) 2014 Git ...
- 09-【el表达式和jstl标签库】
el表达式和jstl标签库 一:el表达式:表达式语言,jsp页面获取数据比较简单1.el表达式的语法(掌握)el表达式通常取值是获取作用域对象中的属性值:${属性名}=>是el表达式的简写的形 ...
- 1. LVS概述
1.LVS介绍 LVS是linux virtual server的简写linux虚拟服务器,是一个虚拟的服务器集群系统,可以再unix/linux平台下实现负载均衡集群功能 2.LVS组成 LVS由2 ...
- 【JAVASCRIPT】call和apply的用法以及区别
function add(c,d){ return this.a + this.b + c + d; } var s = {a:"鸡", b:"你"}; con ...
- Access数据库LIKE问题
access里like的通配符用法是这样:“?”表示任何单一字符: “*”表示零个或多个字符: “#”表示任何一个数字 有时候用%作为通配符不行,需要用*号作为通配符select * from tab ...
- Python——列表赋值的若干用例
原创声明:本文系博主原创文章,转载或引用请注明出处. 1. 直接赋值 >>> a = [1,2,3,4,5] >>> b = a >>> id(a ...
- 网络资源url转化为file对象下载文件
注:只测试过网络图片资源. 一.使用org.apache.commons.io.FileUtils 二. 三.httpURLConnection.disconnect(); 四. import org ...
- JavaScript对象原型
一.MDN上的解释(有点抽象) 基于原型的语言? JavaScript 常被描述为一种基于原型的语言 (prototype-based language)——每个对象拥有一个原型对象,对象以其原型为模 ...