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:

  1. 0 <= indexes.length = sources.length = targets.length <= 100
  2. 0 < indexes[i] < S.length <= 1000
  3. All characters in given inputs are lowercase letters.

Approach #1: String. [Java]

class Solution {
public String findReplaceString(String S, int[] indexes, String[] sources, String[] targets) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < indexes.length; ++i) {
if (S.startsWith(sources[i], indexes[i]))
map.put(indexes[i], i);
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < S.length(); ) {
if (map.containsKey(i)) {
sb.append(targets[map.get(i)]);
i += sources[map.get(i)].length();
} else {
sb.append(S.charAt(i));
i++;
}
}
return sb.toString();
}
}

  

Analysis:

Idea: Use a StringBuilder to build up the result.

1. Iterate through the indexes array and find out all indices that support replacement. Then, store mapping from those index values to their indices in the indexes array into a map named map.

2. Iterate through str, at each iteration i, check whether we can perform replacement, i.e., map.get(i) != null, if yes, append targets[i] to the StringBuilder and increase i by sources[i].length()-1. if no, append str.charAt(i) and increment i.

Reference:

https://leetcode.com/problems/find-and-replace-in-string/discuss/134758/Java-O(n)-solution

833. Find And Replace in String的更多相关文章

  1. 【LeetCode】833. Find And Replace in String 解题报告(Python)

    [LeetCode]833. Find And Replace in String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...

  2. LC 833. Find And Replace in String

    To some string S, we will perform some replacement operations that replace groups of letters with ne ...

  3. 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 ...

  4. String.replace与String.format

    字符串的替换函数replace平常使用的频率非常高,format函数通常用来填补占位符.下面简单总结一下这两个函数的用法. 一.String.replace的两种用法 replace的用法如:repl ...

  5. [转]String.Replace 和 String.ReplaceAll 的区别

    JAVA 中的 replace replaceAll 问题: 测试code System.out.println("1234567890abcdef -----> "+&qu ...

  6. 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 ...

  7. [LeetCode] Find And Replace in String 在字符串中查找和替换

    To some string S, we will perform some replacement operations that replace groups of letters with ne ...

  8. [Swift]LeetCode833. 字符串中的查找与替换 | Find And Replace in String

    To some string S, we will perform some replacement operations that replace groups of letters with ne ...

  9. JAVA中string.replace()和string.replaceAll()的区别及用法

    乍一看,字面上理解好像replace只替换第一个出现的字符(受javascript的影响),replaceall替换所有的字符,其实大不然,只是替换的用途不一样.    public String r ...

随机推荐

  1. js 模拟css3 动画1

    <html> <head> <title> javaScript缓动入门 </title> </head> <body> < ...

  2. json 异常

    com.google.gson.JsonSyntaxException: 1530842820000 1530842820000 是服务器直接返回的Date值由 Gson 解析后出来的值. 后台发出: ...

  3. 关于吴恩达机器学习支持向量机的问题,讲到对偶前有一个最小化f(w)使用拉格朗日求解时转化成一个最大的相等式的理解和一些困惑

    (纯属个人理解) 参考: https://www.zhihu.com/question/267482928 https://www.cnblogs.com/90zeng/p/Lagrange_dual ...

  4. 合并数组,改变原数组apply与不改变原数组

    一看见合并数组,可能第一反应就是concat,concat确实具有我们想要的行为,但它实际上并不附加到现有数组,而是创建并返回一个新数组. 同样你也许会想到ES6的扩展运算符...         但 ...

  5. MFC笔记7

    1.VS中显示行号 工具 -> 选项 -> 文本编辑器 -> C/C++ -> 行号 2.VS中调整字体大小 工具 -> 选项 -> 环境->字体和颜色 3. ...

  6. win10系统goole浏览器安装postMan插件

    1. 首先是下载PostMan工具,可以通过谷歌插件网站查询下载postman插件工具.解压文件 2. 解压压缩包 3. 修改_metadata文件重命名为metadata文件,保存待用.修改后为: ...

  7. servlet中的请求响应与重定向区别

    一.概念 请求响应(转发):将客户端请求转发另一个servlet或者jsp页面------------------------getRequestDispatcher()方法 重定向: 返回一个连接给 ...

  8. cdnbest日志分析显示404的原因

    日志分析报404原因 报这个提示一般是日志没有开启或还没有日志 检查下面几点: 开启和关闭站点日志在节点3311中显示如下图nolog是关闭  日志翻转目前默认是1个小时以后会默认改成20分钟,翻转时 ...

  9. elasticsearch5安装

    环境: centos7 es 5.4.3 es安装 一.下载 wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsear ...

  10. mysql学习笔记--数据库单表查询

    一.查询语句 1.  select [选项] 列名 [from 表名]  [where 条件]  [order by 排序]  [group by 分组]  [having 条件]  [limit 限 ...