-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathFind And Replace in String.java
31 lines (26 loc) · 1.05 KB
/
Find And Replace in String.java
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
class Solution {
public String findReplaceString(String s, int[] indices, String[] sources, String[] targets) {
HashMap<Integer, String> subst = new HashMap<>();
HashMap<Integer, String> tgt = new HashMap<>();
for(int i = 0; i< indices.length; i++) {
subst.put(indices[i], sources[i]);
tgt.put(indices[i],targets[i]);
}
Arrays.sort(indices);
String res = "";
int count = 0;
int avail[] = new int[indices.length];
for(int i = 0; i< s.length(); i++) {
if(count < indices.length && i == indices[count] && s.indexOf(subst.get(indices[count]), indices[count]) == indices[count]){
res = res+""+tgt.get(indices[count]);
i = i+ subst.get(indices[count]).length()-1;
count++;
} else {
if(count < indices.length && i == indices[count])
count++;
res+= s.charAt(i);
}
}
return res;
}
}