Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 704766b

Browse files
Add solution for Find And Replace in String
1 parent 59f2351 commit 704766b

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Algorithm exercises from LeetCode implemented in Java v11.
88
- Student Attendance Record I | [Problem](https://leetcode.com/problems/student-attendance-record-i) | [Solution](src/solutions/StudentAttendanceRecord.java)
99
- Reverse Words in a String III | [Problem](https://leetcode.com/problems/reverse-words-in-a-string-iii) | [Solution](src/solutions/ReverseWordsInStringIII.java)
1010
- One Edit Distance | [Problem](https://leetcode.com/problems/one-edit-distance) | [Solution](src/solutions/OneEditDistance.java)
11+
- Find And Replace in String | [Problem](https://leetcode.com/problems/find-and-replace-in-string) | [Solution](src/solutions/FindAndReplaceInString.java)
1112

1213
### Array
1314
- Jewels and Stones | [Problem](https://leetcode.com/problems/jewels-and-stones) | [Solution](src/solutions/JewelsAndStones.java)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package solutions;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
// [Problem] https://leetcode.com/problems/find-and-replace-in-string
8+
class FindAndReplaceInString {
9+
// String and sorting
10+
// O(nlogn) time, O(n) space
11+
public String findReplaceString(String s, int[] indices, String[] sources, String[] targets) {
12+
Map<Integer, Integer> indexMap = new HashMap<>();
13+
for (int i = 0; i < indices.length; i++) {
14+
indexMap.put(indices[i], i);
15+
}
16+
Arrays.sort(indices);
17+
StringBuilder result = new StringBuilder();
18+
int pointer = 0;
19+
for (int currentIndex : indices) {
20+
int i = indexMap.get(currentIndex);
21+
String source = sources[i];
22+
int nextIndex = currentIndex + source.length();
23+
if (source.equals(s.substring(currentIndex, nextIndex))) {
24+
result.append(s.substring(pointer, currentIndex));
25+
result.append(targets[i]);
26+
pointer = nextIndex;
27+
}
28+
}
29+
result.append(s.substring(pointer));
30+
return result.toString();
31+
}
32+
33+
// Test
34+
public static void main(String[] args) {
35+
FindAndReplaceInString solution = new FindAndReplaceInString();
36+
37+
String s = "abcd";
38+
int[] indices = {0, 2};
39+
String[] sources = {"a", "cd"};
40+
String[] targets = {"eee", "ffff"};
41+
String expectedOutput = "eeebffff";
42+
String actualOutput = solution.findReplaceString(s, indices, sources, targets);
43+
44+
System.out.println("Test passed? " + expectedOutput.equals(actualOutput));
45+
}
46+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /