Skip to content

Commit

Permalink
update 524 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Oct 3, 2024
1 parent 9c2db8a commit 4ead8f9
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,64 @@ public class LongestWordInDictionaryThroughDeleting {

// V0
// TODO: implement
// public String findLongestWord(String s, List<String> dictionary) {
//
// }
public String findLongestWord(String s, List<String> dictionary) {
if (dictionary.size() == 0 && s != null){
return "";
}

List<String> collected = new ArrayList<>();
// check
for (String item : dictionary){
//Map<String, Integer> curMap = this.getElementCount(item);
if (canForm(item, s)){
collected.add(item);
}
}
System.out.println(">>> collected = " + collected);

if (collected.size()==0){
return "";
}

Collections.sort(collected, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
/**
* // First compare by length
* // NOTE !! inverse order, e.g. longest str at first
*/
int lengthComparison = Integer.compare(o2.length(), o1.length());
/**
* // If lengths are equal, compare lexicographically
* // NOTE !!! if lengths are the same, we compare lexicographically
*/
if (lengthComparison == 0) {
return o1.compareTo(o2); // lexicographical order
}
return lengthComparison; // sort by length
}
});

System.out.println(">>> (sorted) collected = " + collected);
return collected.get(0);
}

/**
* NOTE !!!
*
* x : str in dict
* y : s
*
* ref : isSubsequence_3(str, s)
*/
private boolean canForm(String x, String y){
int j = 0;
for (int i = 0; i < y.length() && j < x.length(); i++)
if (x.charAt(j) == y.charAt(i))
j++;
return j == x.length();
}


// V1_1
// IDEA : Brute Force
Expand Down
59 changes: 33 additions & 26 deletions leetcode_java/src/main/java/dev/workspace5.java
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ public String findLongestWord(String s, List<String> dictionary) {
// check
for (String item : dictionary){
//Map<String, Integer> curMap = this.getElementCount(item);
if (canForm(s, item)){
if (canForm(item, s)){
collected.add(item);
}
}
Expand Down Expand Up @@ -813,32 +813,39 @@ private Map<String, Integer> getElementCount(String s){
return map;
}

private boolean canForm(String s, String item){
Map<String, Integer> sMap = this.getElementCount(s);
Map<String, Integer> curMap = this.getElementCount(item);
for (String k: curMap.keySet()){
if(!sMap.keySet().contains(k)){
return false;
}
}
int idx_s = 0;
int idx_i = 0;
while(idx_s < s.length() && idx_i < item.length()){

if (idx_s >= s.length() || idx_i >= item.length()){
return false;
}
// isSubsequence_3(str, s)
private boolean canForm(String x, String y){
// Map<String, Integer> sMap = this.getElementCount(s);
// Map<String, Integer> curMap = this.getElementCount(item);
// for (String k: curMap.keySet()){
// if(!sMap.keySet().contains(k)){
// return false;
// }
// }
// int idx_s = 0;
// int idx_i = 0;
// while(idx_s < s.length() && idx_i < item.length()){
//
// if (idx_s >= s.length() || idx_i >= item.length()){
// return false;
// }
//
// if(s.charAt(idx_s) != item.charAt(idx_i)){
// return false;
// }
// while(s.charAt(idx_s) == item.charAt(idx_i)){
// idx_i += 1;
// }
// idx_i += 1;
// idx_s += 1;
// }
// return true;

if(s.charAt(idx_s) != item.charAt(idx_i)){
return false;
}
while(s.charAt(idx_s) == item.charAt(idx_i)){
idx_i += 1;
}
idx_i += 1;
idx_s += 1;
}
return true;
int j = 0;
for (int i = 0; i < y.length() && j < x.length(); i++)
if (x.charAt(j) == y.charAt(i))
j++;
return j == x.length();
}

}

0 comments on commit 4ead8f9

Please sign in to comment.