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 ...
随机推荐
- .net core中使用Quartz任务调度
使用xml配置Quartz任务调度程序 1.Nuget Install-Package Quartz Install-Package Quartz.Plugins 2.站点根目录下加入文件quartz ...
- oracle instantclient_12_2安装
下载地址 http://www.oracle.com/technetwork/database/database-technologies/instant-client/downloads/index ...
- RS232、RS485和TTL电平与串行通信
RS232.RS485和TTL 作为一个底层软件开发工程师,经常会碰到RS232.RS485和TTL这一类的问题. 之前总是碰到问题之后Google一下,把当下的问题解决了之后就不管了,过个一两天就忘 ...
- WPF 控件库——带有惯性的ScrollViewer*(转)
转:https://blog.csdn.net/ahilll/article/details/82418892 一.先看看效果 二.原理 虽然效果很简单,但是网上的一些资料涉及的代码量非常可观,而且效 ...
- python练习题(四)
题目: 根据一个字符串返回一个ip数组,按照ip最后一位排序, 字符串: str = 'ss192.0.0.12?!289.0.0.1!0.0.0.0!192.163.10.28?192.0.0.5' ...
- Dubbo源码分析:ProxyFactory
roxyFactory将对外开放的服务进行封装.这里使用到代理的方式.ProxyFactory接口有两个不同的实现类:JavassistProxyFactory和JdkProxyFactory.Jdk ...
- 自动化发送邮件之SMTP
一.思路 1.若是QQ邮箱需要在设置-账户里面开启服务 2.在python中smtplib库是专门用来处理邮件 3.自动化邮件发送实操 a.要处理的邮件主题,寄件人,收件人,邮件正文,附件, b.邮件 ...
- 开箱一个docker
1.docker 的出现? 1.1.环境切换配置麻烦 通常我们在开发环境写好代码,打个war/jar包,扔到tomcat下,就算是跑起来了:但是扔到生产环境就挂了,what?各种错误... 1.2.应 ...
- robot framework的使用方法
1.后台代码: 目录结构: 测试代码:Arithmetic.py 2.开始编写用例 直接在eclipse上新建一个txt文件即可,或者是通过ride编写用例. (1).首先在eclipse上新建目录T ...
- MATLAB中运算符优先级
下述运算符的优先级从低到高: 1.先决或(||): 2.先决与(&&): 3.逻辑或(|): 4.逻辑与(&): 5.等于类(<,<=,>,>=,==, ...