Skip to content

Commit 84e7a26

Browse files
committed
update 833 java
1 parent 83fed7d commit 84e7a26

File tree

2 files changed

+96
-20
lines changed

2 files changed

+96
-20
lines changed

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,46 @@ public class FindAndReplaceInString {
8181
//
8282
// }
8383

84+
// V0-1
85+
// IDEA : MAP + startsWith (fixed by GPT)
86+
public String findReplaceString_0_1(String s, int[] indices, String[] sources, String[] targets) {
87+
// Map to store valid replacement indices and their respective replacement info
88+
Map<Integer, Integer> map = new HashMap<>();
89+
90+
// Collect all validated replacements
91+
for (int i = 0; i < indices.length; i++) {
92+
if (s.startsWith(sources[i], indices[i])) {
93+
map.put(indices[i], i);
94+
}
95+
}
96+
97+
// Construct the updated string
98+
StringBuilder sb = new StringBuilder();
99+
for (int i = 0; i < s.length(); ) {
100+
if (map.containsKey(i)) {
101+
int replacementIndex = map.get(i);
102+
sb.append(targets[replacementIndex]); // Add the replacement
103+
/**
104+
* NOTE !!!
105+
*
106+
* update index with replacement string length
107+
*
108+
*/
109+
i += sources[replacementIndex].length(); // Skip the replaced substring
110+
} else {
111+
sb.append(s.charAt(i)); // Add the original character
112+
/**
113+
* NOTE !!!
114+
*
115+
* move index 1 to right (i = i +1)
116+
*/
117+
i++;
118+
}
119+
}
120+
121+
return sb.toString();
122+
}
123+
84124
// V1
85125
// IDEA : HASHMAP
86126
// https://leetcode.com/problems/find-and-replace-in-string/submissions/1454170265/

leetcode_java/src/main/java/dev/workspace5.java

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3197,34 +3197,70 @@ public int maxSubArray(int[] nums) {
31973197
// 6.50 - 7.10 pm
31983198
public String findReplaceString(String s, int[] indices, String[] sources, String[] targets) {
31993199

3200-
if (indices.length == 0){
3201-
return s;
3202-
}
3203-
3204-
// re-order indices
3205-
Arrays.sort(indices);
3200+
Map<Integer, String> map = new HashMap<>();
3201+
String[] sArray = s.split("");
32063202

3207-
// check if overlap
3208-
//boolean isOverlap = false;
3209-
if (isOverlap(s, indices)){
3210-
return s;
3203+
// collect all validated replacement
3204+
for (int i = 0; i < sources.length; i++){
3205+
String target = targets[i];
3206+
String source = sources[i];
3207+
// public boolean startsWith(String prefix, int toffset) {}
3208+
if (s.startsWith(source, indices[i])){
3209+
//map.put(indices[i], target);
3210+
map.put(indices[i], target);
3211+
}
32113212
}
32123213

3213-
Map<Integer, String> map = new HashMap<>();
3214-
for (int i = 0; i < indices.length; i++){
3215-
map.put(i, targets[i]);
3216-
}
3214+
System.out.println(">>> map = " + map);
32173215

3218-
String res = "";
3219-
String[] sArray = s.split("");
3220-
for (int j = indices.length; j > 0; j++){
3221-
sArray[j] = map.get(j);
3216+
// update string
3217+
StringBuilder sb = new StringBuilder();
3218+
for (int j = 0; j < sArray.length;){
3219+
if (map.containsKey(j)){
3220+
sb.append(map.get(j));
3221+
// NOTE !!!
3222+
j += sources[j].length();
3223+
}else{
3224+
sb.append(sArray[j]);
3225+
//j = j + sArray[j].length();
3226+
j += 1;
3227+
}
32223228
}
32233229

3224-
// array -> string
3225-
return sArray.toString(); // ?
3230+
return sb.toString();
32263231
}
32273232

3233+
3234+
// public String findReplaceString(String s, int[] indices, String[] sources, String[] targets) {
3235+
//
3236+
// if (indices.length == 0){
3237+
// return s;
3238+
// }
3239+
//
3240+
// // re-order indices
3241+
// Arrays.sort(indices);
3242+
//
3243+
// // check if overlap
3244+
// //boolean isOverlap = false;
3245+
// if (isOverlap(s, indices)){
3246+
// return s;
3247+
// }
3248+
//
3249+
// Map<Integer, String> map = new HashMap<>();
3250+
// for (int i = 0; i < indices.length; i++){
3251+
// map.put(i, targets[i]);
3252+
// }
3253+
//
3254+
// String res = "";
3255+
// String[] sArray = s.split("");
3256+
// for (int j = indices.length; j > 0; j++){
3257+
// sArray[j] = map.get(j);
3258+
// }
3259+
//
3260+
// // array -> string
3261+
// return sArray.toString(); // ?
3262+
// }
3263+
32283264
private boolean isOverlap(String s, int[] indices){
32293265
return false;
32303266
}

0 commit comments

Comments
 (0)