Repository navigation
Expand file tree
/
Copy pathFindAndReplaceString.java
More file actions
82 lines (56 loc) · 2.71 KB
/
Copy pathFindAndReplaceString.java
File metadata and controls
82 lines (56 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.abn.dsalgos.problems;
import org.testng.Assert;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/*
You are given a 0-indexed string s that you must perform k replacement operations on. The replacement operations
are given as three 0-indexed parallel arrays, indices, sources, and targets, all of length k.
To complete the ith replacement operation:
Check if the substring sources[i] occurs at index indices[i] in the original string s.
If it does not occur, do nothing.
Otherwise if it does occur, replace that substring with targets[i].
For example, if s = "abcd", indices[i] = 0, sources[i] = "ab", and targets[i] = "eee", then the result of this replacement
will be "eeecd".
All replacement operations must occur simultaneously, meaning the replacement operations should not affect the indexing of
each other. The testcases will be generated such that the replacements will not overlap.
For example, a testcase with s = "abc", indices = [0, 1], and sources = ["ab","bc"] will not be generated because the
"ab" and "bc" replacements overlap.
Return the resulting string after performing all replacement operations on s.
A substring is a contiguous sequence of characters in a string.
Input: s = "abcd", indices = [0, 2], sources = ["a", "cd"], targets = ["eee", "ffff"]
Output: "eeebffff"
Input: s = "abcd", indices = [0, 2], sources = ["ab","ec"], targets = ["eee","ffff"]
Output: "eeecd"
*/
public class FindAndReplaceString {
public String findReplaceString(String s, int[] indices, String[] sources, String[] targets) {
int sourceLen = 0;
Map<Integer, String> sourceMap = new HashMap<>();
Map<Integer, String> targetMap = new HashMap<>();
for(int i = 0; i< sources.length; i ++) {
sourceMap.put(indices[i], sources[i]);
targetMap.put(indices[i], targets[i]);
}
Arrays.sort(indices);
StringBuilder sb = new StringBuilder(s);
for(int i = indices.length -1; i >= 0; i --) {
int index = indices[i];
String sourceStr = sourceMap.get(index);
String targetStr = targetMap.get(index);
sourceLen = sourceStr.length();
if(s.substring(index,sourceLen + index).equals(sourceStr)) {
sb.replace(index, sourceLen + index, targetStr);
}
}
return sb.toString();
}
public static void main(String[] args) {
String s = "vmokgggqzp";
int[] indices = {3,5,1};
String[] sources = {"kg","ggq","mo"};
String[] target = {"s","so","bfr"};
String expected = "vbfrssozp";
Assert.assertEquals(new FindAndReplaceString().findReplaceString(s, indices, sources, target), expected);
}
}