Skip to content

Commit 760e052

Browse files
committed
update 833 java
1 parent 0f7874c commit 760e052

File tree

1 file changed

+101
-1
lines changed

1 file changed

+101
-1
lines changed

leetcode_java/src/main/java/LeetCodeJava/String/FindAndReplaceInString.java

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
// https://leetcode.com/problems/find-and-replace-in-string/description/
44

5+
import java.util.Arrays;
56
import java.util.HashMap;
67
import java.util.Map;
78

@@ -59,6 +60,7 @@
5960
public class FindAndReplaceInString {
6061

6162
// V0
63+
// TODO : implement
6264
// public String findReplaceString(String s, int[] indices, String[] sources, String[] targets) {
6365
//
6466
// }
@@ -67,12 +69,44 @@ public class FindAndReplaceInString {
6769
// IDEA : HASHMAP
6870
// https://leetcode.com/problems/find-and-replace-in-string/submissions/1454170265/
6971
public String findReplaceString_1(String s, int[] indices, String[] sources, String[] targets) {
72+
/**
73+
* • Purpose:
74+
* • Create a map where the key is the index in the string s where a valid replacement can occur, and the value is the index in the sources and targets arrays.
75+
*
76+
* • Logic:
77+
* • Loop through each replacement instruction (indices, sources, targets).
78+
* • Check if sources[i] exists as a substring of s starting at indices[i]:
79+
* • Use s.startsWith(sources[i], indices[i]) to verify this condition.
80+
* • If the condition is true, store the index (indices[i]) in the map with its corresponding sources/targets index (i).
81+
*
82+
* • Result:
83+
* • The map will only contain valid replacement indices, ensuring that only valid replacements are performed.
84+
*
85+
*/
7086
Map<Integer, Integer> map = new HashMap<>();
7187
for (int i = 0; i < indices.length; i++) {
7288
if (s.startsWith(sources[i], indices[i])) {
7389
map.put(indices[i], i);
7490
}
7591
}
92+
93+
/**
94+
* • Purpose:
95+
* • Construct the final string using a StringBuilder, processing each character of s sequentially.
96+
*
97+
* • Logic:
98+
* • Case 1: If the current index i is not in the map, it means no replacement starts at this index.
99+
* • Append the character at s[i] to the StringBuilder and move to the next character (i++).
100+
* • Case 2: If the current index i is in the map, it means a valid replacement starts here.
101+
* • Retrieve the corresponding index from map.get(i) and:
102+
* • Append the replacement string (targets[map.get(i)]) to the StringBuilder.
103+
* • Skip over the characters in the source substring (i += sources[map.get(i)].length()).
104+
*
105+
*
106+
* • Why Use StringBuilder:
107+
* • Strings in Java are immutable, so using StringBuilder allows efficient in-place concatenation.
108+
*
109+
*/
76110
StringBuilder sb = new StringBuilder();
77111
for (int i = 0; i < s.length(); ) {
78112
if (!map.containsKey(i)) {
@@ -87,9 +121,72 @@ public String findReplaceString_1(String s, int[] indices, String[] sources, Str
87121
}
88122

89123
// V2
124+
// IDEA : HASHMAP (gpt)
125+
/**
126+
* 1. Sorting the Indices:
127+
* • The replacements must not interfere with each other. For example, if you replace earlier substrings, it can shift the later indices.
128+
* • To avoid this issue, the indices are processed in descending order.
129+
*
130+
* 2. String Replacement:
131+
* • For each replacement index i, check if the substring starting at indices[i] in s matches sources[i] using s.startsWith(source, start).
132+
* • If it matches, replace the substring from start to start + source.length() with targets[i].
133+
*
134+
* 3. StringBuilder for Efficient Modifications:
135+
* • Use StringBuilder to perform in-place modifications on the string, which is more efficient than creating new strings repeatedly.
136+
*
137+
*/
138+
public String findReplaceString_2(String s, int[] indices, String[] sources, String[] targets) {
139+
// Create an array of indices and sort them in descending order
140+
/**
141+
*
142+
* - To avoid affecting the indices of unprocessed replacements, we process the replacements in reverse order of their indices.
143+
* - Sorting the indices ensures that replacements starting from the largest index occur first.
144+
*
145+
*
146+
* Why Reverse Order:
147+
* • For example, consider s = "abcd", indices = [0, 2], sources = ["a", "cd"], and targets = ["eee", "ffff"].
148+
* If we replace "a" (at index 0) first, it would shift the position of "cd", causing the second replacement to fail.
149+
* Replacing "cd" first avoids this issue.
150+
*/
151+
Integer[] sortedIndices = new Integer[indices.length];
152+
for (int i = 0; i < indices.length; i++) {
153+
sortedIndices[i] = i;
154+
}
155+
Arrays.sort(sortedIndices, (a, b) -> Integer.compare(indices[b], indices[a]));
156+
157+
// Perform replacements
158+
/**
159+
* • Using StringBuilder:
160+
* • Strings in Java are immutable, meaning every modification creates a new string.
161+
* • Using StringBuilder allows us to modify the string efficiently in-place.
162+
*
163+
* • Checking Substring Match:
164+
* • The method s.startsWith(source, start) checks whether the substring at index start matches the source string.
165+
* • If it matches, we proceed to replace it with the target.
166+
*
167+
* • Replacing Substrings:
168+
* • The method sb.replace(start, start + source.length(), target) replaces the substring from start to start + source.length() with the target.
169+
*/
170+
StringBuilder sb = new StringBuilder(s);
171+
for (int idx : sortedIndices) {
172+
int start = indices[idx];
173+
String source = sources[idx];
174+
String target = targets[idx];
175+
176+
// Check if `source` exists at `start`
177+
if (s.startsWith(source, start)) {
178+
sb.replace(start, start + source.length(), target);
179+
}
180+
}
181+
182+
return sb.toString();
183+
}
184+
185+
186+
// V3
90187
// TODO : replacer `Pair` in code
91188
// https://leetcode.com/problems/find-and-replace-in-string/submissions/1454169549/
92-
// public String findReplaceString_2(String s, int[] indices, String[] sources, String[] targets) {
189+
// public String findReplaceString_3(String s, int[] indices, String[] sources, String[] targets) {
93190
// Map<Integer , Pair> replacements = new TreeMap<>();
94191
// StringBuilder res = new StringBuilder();
95192
//
@@ -110,4 +207,7 @@ public String findReplaceString_1(String s, int[] indices, String[] sources, Str
110207
// return res.toString();
111208
// }
112209

210+
// V3
211+
// https://blog.csdn.net/qq_37821701/article/details/125737152
212+
113213
}

0 commit comments

Comments
 (0)