Skip to content

Commit

Permalink
update 833 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 18, 2024
1 parent 83fed7d commit 84e7a26
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,46 @@ public class FindAndReplaceInString {
//
// }

// V0-1
// IDEA : MAP + startsWith (fixed by GPT)
public String findReplaceString_0_1(String s, int[] indices, String[] sources, String[] targets) {
// Map to store valid replacement indices and their respective replacement info
Map<Integer, Integer> map = new HashMap<>();

// Collect all validated replacements
for (int i = 0; i < indices.length; i++) {
if (s.startsWith(sources[i], indices[i])) {
map.put(indices[i], i);
}
}

// Construct the updated string
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); ) {
if (map.containsKey(i)) {
int replacementIndex = map.get(i);
sb.append(targets[replacementIndex]); // Add the replacement
/**
* NOTE !!!
*
* update index with replacement string length
*
*/
i += sources[replacementIndex].length(); // Skip the replaced substring
} else {
sb.append(s.charAt(i)); // Add the original character
/**
* NOTE !!!
*
* move index 1 to right (i = i +1)
*/
i++;
}
}

return sb.toString();
}

// V1
// IDEA : HASHMAP
// https://leetcode.com/problems/find-and-replace-in-string/submissions/1454170265/
Expand Down
76 changes: 56 additions & 20 deletions leetcode_java/src/main/java/dev/workspace5.java
Original file line number Diff line number Diff line change
Expand Up @@ -3197,34 +3197,70 @@ public int maxSubArray(int[] nums) {
// 6.50 - 7.10 pm
public String findReplaceString(String s, int[] indices, String[] sources, String[] targets) {

if (indices.length == 0){
return s;
}

// re-order indices
Arrays.sort(indices);
Map<Integer, String> map = new HashMap<>();
String[] sArray = s.split("");

// check if overlap
//boolean isOverlap = false;
if (isOverlap(s, indices)){
return s;
// collect all validated replacement
for (int i = 0; i < sources.length; i++){
String target = targets[i];
String source = sources[i];
// public boolean startsWith(String prefix, int toffset) {}
if (s.startsWith(source, indices[i])){
//map.put(indices[i], target);
map.put(indices[i], target);
}
}

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

String res = "";
String[] sArray = s.split("");
for (int j = indices.length; j > 0; j++){
sArray[j] = map.get(j);
// update string
StringBuilder sb = new StringBuilder();
for (int j = 0; j < sArray.length;){
if (map.containsKey(j)){
sb.append(map.get(j));
// NOTE !!!
j += sources[j].length();
}else{
sb.append(sArray[j]);
//j = j + sArray[j].length();
j += 1;
}
}

// array -> string
return sArray.toString(); // ?
return sb.toString();
}


// public String findReplaceString(String s, int[] indices, String[] sources, String[] targets) {
//
// if (indices.length == 0){
// return s;
// }
//
// // re-order indices
// Arrays.sort(indices);
//
// // check if overlap
// //boolean isOverlap = false;
// if (isOverlap(s, indices)){
// return s;
// }
//
// Map<Integer, String> map = new HashMap<>();
// for (int i = 0; i < indices.length; i++){
// map.put(i, targets[i]);
// }
//
// String res = "";
// String[] sArray = s.split("");
// for (int j = indices.length; j > 0; j++){
// sArray[j] = map.get(j);
// }
//
// // array -> string
// return sArray.toString(); // ?
// }

private boolean isOverlap(String s, int[] indices){
return false;
}
Expand Down

0 comments on commit 84e7a26

Please sign in to comment.