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 ...
随机推荐
- idea上maven打包
首先要实现maven打包,pom需要引入依赖 pom.xml <project> <dependencies> …… </dependencies> <bui ...
- 关于小程序授权地理位置(wx.getLocation + 用户体验)
wx.getLocation 如果用户曾点击过一次 “确认授权” , 那么再次调用该接口时将不会出现弹出框(可以直接拿到经纬度) 关于用户体验: 在 onLoad 中判断: 如果用户之前“没有触发过“ ...
- Linux命令——mount、umount
前言 由于引入了LVM.RAID技术,导致OS时别到的磁盘已经不单纯是事实意义上的物理磁盘(虽然OS认为他是物理盘).传统文件系统与分区可以认为是1:1关系,但是现在一个分区可以有多个FS,一个FS也 ...
- nginx的rewrite跳转
Rewrite标记flag
- c风格的字符串
c风格的字符串的标注库 #include <cstring> 使用c 风格的字符串,牢记,其必须以null为结束标志 如 char ca[]={'c','+','='}; cout< ...
- 详细点的Mysql主从同步
.说明 此操作文档,如果在master机器已开启bin-log及设定好server-id的情况下,可以不锁表,不停机的实现master-slave同步.这一同步可以将master上已有数据同步到sla ...
- SD介绍
1. 介绍 MMC,MultiMediaCard,即多媒体卡,是一种非易失性存储器件,有7pin,目前已基本被SD卡代替 eMMC,Embedded Multimedia Card,内嵌式存储器,以B ...
- adb命令过滤w级别日志命令
adb logcat *:W 过滤某关键字日志 adb logcat *:W | find "woyihome" 过滤某关键字日志,生成txt文档 adb logcat *:W | ...
- python开发应用-本地数据获取方法
文件的打开.读写和关闭 文件的打开: file_obj=open(filename,mode='r',buffering=-1,...) filename是强制参数 mode是可选参数,默认值是r b ...
- GitLab CI runner can't connect to tcp://localhost:2375 in kubernetes
报错的.gitlab-ci.yml配置如下 image: docker:latest services: - docker:dind variables: DOCKER_HOST: tcp://loc ...