Skip to content

Commit

Permalink
update 1055 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Sep 2, 2024
1 parent e947196 commit ea8feab
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,59 @@ public class ShortestWayToFormString {
// IDEA : 2 POINTER
// TODO : implement

// V1
// V1_1
// IDEA : 2 POINTER (gpt)
// TODO : validate below
public int shortestWay_1(String source, String target) {
// Step 1: Check if every character in target exists in source
for (char x : target.toCharArray()) {
if (source.indexOf(x) == -1) {
return -1;
}
}

int sourceLen = source.length();
int targetLen = target.length();
int sourceIdx = 0;
int targetIdx = 0;
int res = 0;

// Step 2: Iterate through the target string
while (targetIdx < targetLen) {
int currentIdx = targetIdx;

// Step 3: Match as many characters as possible from source with target
/** NOTE !!! below logic
*
* while src idx < src len && target idx < target len.
* keep comparing src and target val
*/
while (sourceIdx < sourceLen && targetIdx < targetLen) {
if (source.charAt(sourceIdx) == target.charAt(targetIdx)) {
targetIdx++;
}
sourceIdx++;
}

// Step 4: If no progress was made in this pass, it means the target cannot be formed
if (targetIdx == currentIdx) {
return -1;
}

// Step 5: Reset source index and increment the subsequence count
/** NOTE !!! reset src idx after src idx reach src len */
sourceIdx = 0;
res++;
}

return res;
}


// V2_1
// https://leetcode.ca/2018-10-20-1055-Shortest-Way-to-Form-String/
// IDEA : 2 POINTER
public int shortestWay_1(String source, String target) {
public int shortestWay_2_1(String source, String target) {
int m = source.length(), n = target.length();
int ans = 0, j = 0;
while (j < n) {
Expand Down
38 changes: 36 additions & 2 deletions leetcode_java/src/main/java/dev/workspace3.java
Original file line number Diff line number Diff line change
Expand Up @@ -6916,7 +6916,7 @@ public void setDist(int dist) {
// LC 1055
// https://leetcode.ca/all/1055.html
// bfs
public int shortestWay(String source, String target) {
public int shortestWay_0(String source, String target) {

// check if target has element NOT existrd in soruce
Set<String> srcSet = new HashSet<>();
Expand Down Expand Up @@ -6955,7 +6955,41 @@ public int shortestWay(String source, String target) {
return cnt;
}

// LC 809
// LC 1055
// https://leetcode.ca/all/1055.html
// bfs
public int shortestWay(String source, String target) {

for (String x : target.split("")){
if (!source.contains(x)) {
return -1;
}
}

String[] src_list = source.split("");
String[] target_list = target.split("");

int res = 0;
boolean sameSubStr = true;

for (int i = 0; i < target.length(); i++){
//int j = Arrays.asList(src_list).indexOf(target_list[i]);
int j = 1;
while (!target_list[i].equals(src_list[j]) && j < target.length()){
j += 1;
sameSubStr = false;
}
if(!sameSubStr){
res += 1;
j = 1;
}
}

return res > 0 ? res : - 1;
}


// LC 809
// https://leetcode.com/problems/expressive-words/
// 2 pointers
public int expressiveWords(String s, String[] words) {
Expand Down

0 comments on commit ea8feab

Please sign in to comment.