From ea8feabf4ec0bbef68ecde7160a85e60dc888723 Mon Sep 17 00:00:00 2001 From: yennanliu Date: Mon, 2 Sep 2024 22:05:14 +0800 Subject: [PATCH] update 1055 java --- .../TwoPointer/ShortestWayToFormString.java | 53 ++++++++++++++++++- .../src/main/java/dev/workspace3.java | 38 ++++++++++++- 2 files changed, 87 insertions(+), 4 deletions(-) diff --git a/leetcode_java/src/main/java/LeetCodeJava/TwoPointer/ShortestWayToFormString.java b/leetcode_java/src/main/java/LeetCodeJava/TwoPointer/ShortestWayToFormString.java index e82bee79..31d32c2b 100644 --- a/leetcode_java/src/main/java/LeetCodeJava/TwoPointer/ShortestWayToFormString.java +++ b/leetcode_java/src/main/java/LeetCodeJava/TwoPointer/ShortestWayToFormString.java @@ -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) { diff --git a/leetcode_java/src/main/java/dev/workspace3.java b/leetcode_java/src/main/java/dev/workspace3.java index 173bbf9e..4dbb1beb 100644 --- a/leetcode_java/src/main/java/dev/workspace3.java +++ b/leetcode_java/src/main/java/dev/workspace3.java @@ -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 srcSet = new HashSet<>(); @@ -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) {