Leetcode: 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.
HashMap
class Solution {
public String findReplaceString(String S, int[] indexes, String[] sources, String[] targets) {
StringBuilder res = new StringBuilder(); HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < indexes.length; i ++) {
map.put(indexes[i], i);
} for (int i = 0, matchLen = 0; i < S.length(); i += matchLen) {
matchLen = 1; if (map.containsKey(i)) {
int p = map.get(i); // p is the index in indexes[]
if (S.startsWith(sources[p], i)) {
res.append(targets[p]);
matchLen = sources[p].length();
}
else res.append(S.charAt(i));
}
else res.append(S.charAt(i));
}
return res.toString();
}
}
Sort and replace S from right to left (未深究)
public String findReplaceString(String S, int[] indexes, String[] sources, String[] targets) {
List<int[]> sorted = new ArrayList<>();
for (int i = 0 ; i < indexes.length; i++) sorted.add(new int[]{indexes[i], i});
Collections.sort(sorted, Comparator.comparing(i -> -i[0]));
for (int[] ind: sorted) {
int i = ind[0], j = ind[1];
String s = sources[j], t = targets[j];
if (S.substring(i, i + s.length()).equals(s)) S = S.substring(0, i) + t + S.substring(i + s.length());
}
return S;
}
Leetcode: Find And Replace in String的更多相关文章
- [LeetCode] Find And Replace in String 在字符串中查找和替换
To some string S, we will perform some replacement operations that replace groups of letters with ne ...
- 【LeetCode】833. Find And Replace in String 解题报告(Python)
[LeetCode]833. Find And Replace in String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...
- LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation
LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...
- [LeetCode] 415. Add Strings_Easy tag: String
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...
- 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 ...
- 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 —— weekly contest 84
Find And Replace in String To some string S, we will perform some replacement operations that replac ...
随机推荐
- unittest使用
unittest:单元测试框架主要包含四部分: 1.测试固件(test fixture): 定义:包含执行测试前的准备setUP().测试执行完后的清扫工作tearDown() 注意: setUp() ...
- JavaScript: 数据类型检测
由于JavaScript是门松散类型语言,定义变量时没有类型标识信息,并且在运行期可以动态更改其类型,所以一个变量的类型在运行期是不可预测的,因此,数据类型检测在开发当中就成为一个必须要了解和掌握的知 ...
- vue2 手记
vue2 手记 Vue文档:https://cn.vuejs.org/v2/api/#provide-inject Vue 生命周期:https://cn.vuejs.org/v2/guide/ins ...
- Notepad++常用快捷键:
Ctrl-H 打开Find / Replace 对话框 Ctrl-D 复制当前行 Ctrl-L 删除当前行 Ctrl-T 上下行交换 F3 找下一个 Shift-F3 找上一个 Ctrl-Shift- ...
- docker学习1-CentOS 7安装docker环境
前言 Docker 提供轻量的虚拟化,你能够从Docker获得一个额外抽象层,你能够在单台机器上运行多个Docker微容器,而每个微容器里都有一个微服务或独立应用,例如你可以将Tomcat运行在一个D ...
- http消息与webservice
别人的:在一台配置较低的PC上,同时开启服务端与客户端,10000条数据,使用基于http的消息逐条进行传递,从开始传递至全部接收并处理完毕,大概需要465秒的时间:而在同一台机器上,使用WebSer ...
- mongodb 简单使用说明
首先安装 mongodb软件地址 https://www.mongodb.org/downloads#production: 然后在 mongodb安装目录下找到bin 文件夹进去 在它的位置上按下 ...
- C语言 define实现的宏函数汇总
最大值,最小值 #define MAX( x, y ) ( (x) > (y) ? (x) : (y) )#define MIN( x, y ) ( (x) < (y) ? (x) : ( ...
- pass的作用?
1.空语句 do nothing 2.保证格式完整,保证语义完整 3.占位语句
- EFK架构图
Environment:{ 三台CentOS7操作系统 (环境均安装jdk) } 需要机器: 消息中间件的机器中 kafka 和 zookeeper 同时安装在三台虚拟机 logstash 960 ...