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:

  1. Input: S = "abcd", indexes = [0,2], sources = ["a","cd"], targets = ["eee","ffff"]
  2. Output: "eeebffff"
  3. Explanation: "a" starts at index 0 in S, so it's replaced by "eee".
  4. "cd" starts at index 2 in S, so it's replaced by "ffff".

Example 2:

  1. Input: S = "abcd", indexes = [0,2], sources = ["ab","ec"], targets = ["eee","ffff"]
  2. Output: "eeecd"
  3. Explanation: "ab" starts at index 0 in S, so it's replaced by "eee".
  4. "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]

  1. class Solution {
  2. public String findReplaceString(String S, int[] indexes, String[] sources, String[] targets) {
  3. Map<Integer, Integer> map = new HashMap<>();
  4. for (int i = 0; i < indexes.length; ++i) {
  5. if (S.startsWith(sources[i], indexes[i]))
  6. map.put(indexes[i], i);
  7. }
  8. StringBuilder sb = new StringBuilder();
  9. for (int i = 0; i < S.length(); ) {
  10. if (map.containsKey(i)) {
  11. sb.append(targets[map.get(i)]);
  12. i += sources[map.get(i)].length();
  13. } else {
  14. sb.append(S.charAt(i));
  15. i++;
  16. }
  17. }
  18. return sb.toString();
  19. }
  20. }

  

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. <记录> HtmlHelper和 强类型页面

    HtmlHelper  路径生成 <!--普通写法--> <a href="/home/index">超链接</a> <!--利用Url类 ...

  2. 基于windows平台搭建elasticsearch

    部署准备 elasticsearch-6.0.1.zip--https://www.elastic.co/downloads/elasticsearch elasticsearch-head-mast ...

  3. 一次sendmsg的改造过程

    比较蛋疼的一个改造过程,简单记录一下. 场景:用户态使用sendmsg发包,tcp报文,由于内核实现过程中存在一次kernel_read,也就是存在将pagecache中的内容拷贝一次的问题. 为了减 ...

  4. Object.create() vs new SomeFunction() in javascript

    Object.create builds an object that inherits directly from the one passed as its first argument. Wit ...

  5. 《Spring_Four》第一次作业:团队亮相

    part one: 1.队名:Spring_Four 2.团队成员组成:学号/姓名(标记团队组长) 201571030114 李蕾 201571030143 周甜甜 201571030139 张天旭( ...

  6. hello1以及hello2的部分代码分析

    (一)1.GreetingServlet.java源码文件: @WebServlet("/greeting") //以@WebServlet注释开头,注释指定相对于上下文根的URL ...

  7. 20162322 朱娅霖 作业011 Hash

    20162322 2017-2018-1 <程序设计与数据结构>第十一周学习总结 教材学习内容总结 哈希方法 一.定义 哈希:次序--更具体来说是项在集合中的位置--由所保存元素值的某个函 ...

  8. C++内存空间

  9. cast

    https://blog.csdn.net/seabeam/article/details/47841539 在UVM中经常可以看到$cast的身影,这是SV的build-in task之一,当然它还 ...

  10. cisco PBR

    access-list 2000 permit ip 10.11.50.0 0.0.0.255 anyaccess-list 2001 permit ip 10.11.50.0 0.0.0.255 1 ...