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 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 <= 100
0 < indexes[i] < S.length <= 1000
- 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的更多相关文章
- 【LeetCode】833. Find And Replace in String 解题报告(Python)
[LeetCode]833. Find And Replace in String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...
- LC 833. Find And Replace in String
To some string S, we will perform some replacement operations that replace groups of letters with ne ...
- 833. Find And Replace in String
To some string S, we will perform some replacement operations that replace groups of letters with ne ...
- 835. Image Overlap —— weekly contest 84
Image Overlap Two images A and B are given, represented as binary, square matrices of the same size. ...
- 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 ...
- 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 ...
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
随机推荐
- 记一次uwsgi django nginx 调优
[uwsgi] project = fortune_cat uid = ubuntu gid = ubuntu path = fortune_cat base = /home/%(uid) chdir ...
- Python面向对象基础变量
In [1]: class A: ...: NAME = 'A' # 类的直接下级作用域 叫做类变量 ...: def init(self, name): ...: self.name = name ...
- 计算(calc)
计算(calc) [题目描述] 小明在你的帮助下,破密了Ferrari设的密码门,正要往前走,突然又出现了一个密码门,门上有一个算式,其中只有"(",")",& ...
- Java知识系统回顾整理01基础05控制流程04 for
一.for 比较for和while public class HelloWorld { public static void main(String[] args) { //使用while打印0到4 ...
- C++ | 继承(基类,父类,超类),(派生类,子类)
转载:https://blog.csdn.net/Sherlock_Homles/article/details/82927515 文章参考:https://blog.csdn.net/war1111 ...
- C++中cstring.h和string.h的区别
转载:https://blog.csdn.net/qian_chun_qiang/article/details/80648691 1.string与cstring有什么区别 <string&g ...
- C++中cout和cerr
参考:https://blog.csdn.net/garfield2005/article/details/7639833 之前一直在用,但就是没在意两者到底有啥却别,今天又想到这个问题,总结下吧(以 ...
- getopt函数用法
getopt被用来解析命令行选项参数. #include <unistd.h> extern char *optarg; //选项的参数指针 extern int o ...
- Azure内容审查器之羞羞图审查
上一篇 Azure 内容审查器之文本审查我们已经介绍了如果使用Azure进行文字内容的审核.对于社区内容,上传的图片是否含有羞羞内容也是需要过虑的.但是最为一般开发者自己很难实现这种级别的智能识别.但 ...
- 对ACE和ATL积分
下载source code - 39.66 KB 介绍 这篇文章展示了一种结合ACE和ATL的方法.它不打算作为功能演示,而是作为一个小型的"入门"解决方案,展示实现此目标的可行方 ...