Skip to content

Commit

Permalink
update 208 java, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Mar 9, 2024
1 parent 4f19ce4 commit e37941d
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 105 deletions.
2 changes: 1 addition & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
20240309: 207(again!!!)
20240309: 207(again!!!),208
20240308: 152,153(again),190,191,198,200,206(again)
20240307: 139(again),141,143(again)
20240306: 121,124(again),125,128,133(again)
Expand Down
18 changes: 9 additions & 9 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
2024-05-03 -> ['207(again!!!)']
2024-05-03 -> ['207(again!!!),208']
2024-05-02 -> ['152,153(again),190,191,198,200,206(again)']
2024-05-01 -> ['139(again),141,143(again)']
2024-04-30 -> ['121,124(again),125,128,133(again)']
Expand All @@ -8,7 +8,7 @@
2024-04-24 -> ['39,48(again),49,53,54']
2024-04-23 -> ['20,21,23,33(again)']
2024-04-22 -> ['1,3,5,4,19']
2024-04-12 -> ['207(again!!!)']
2024-04-12 -> ['207(again!!!),208']
2024-04-11 -> ['152,153(again),190,191,198,200,206(again)']
2024-04-10 -> ['139(again),141,143(again)']
2024-04-09 -> ['121,124(again),125,128,133(again)']
Expand All @@ -18,26 +18,26 @@
2024-04-03 -> ['39,48(again),49,53,54']
2024-04-02 -> ['20,21,23,33(again)']
2024-04-01 -> ['1,3,5,4,19']
2024-03-30 -> ['207(again!!!)']
2024-03-30 -> ['207(again!!!),208']
2024-03-29 -> ['152,153(again),190,191,198,200,206(again)']
2024-03-28 -> ['139(again),141,143(again)']
2024-03-27 -> ['121,124(again),125,128,133(again)']
2024-03-26 -> ['100,102,104,105(again)']
2024-03-25 -> ['73,76,79(again),91,25']
2024-03-24 -> ['55(again),56,62,70']
2024-03-22 -> ['207(again!!!)']
2024-03-22 -> ['207(again!!!),208']
2024-03-21 -> ['152,153(again),190,191,198,200,206(again)', '39,48(again),49,53,54']
2024-03-20 -> ['139(again),141,143(again)', '20,21,23,33(again)']
2024-03-19 -> ['121,124(again),125,128,133(again)', '1,3,5,4,19']
2024-03-18 -> ['100,102,104,105(again)']
2024-03-17 -> ['207(again!!!)', '73,76,79(again),91,25']
2024-03-17 -> ['207(again!!!),208', '73,76,79(again),91,25']
2024-03-16 -> ['152,153(again),190,191,198,200,206(again)', '55(again),56,62,70']
2024-03-15 -> ['139(again),141,143(again)']
2024-03-14 -> ['207(again!!!)', '121,124(again),125,128,133(again)']
2024-03-14 -> ['207(again!!!),208', '121,124(again),125,128,133(again)']
2024-03-13 -> ['152,153(again),190,191,198,200,206(again)', '100,102,104,105(again)', '39,48(again),49,53,54']
2024-03-12 -> ['207(again!!!)', '139(again),141,143(again)', '73,76,79(again),91,25', '20,21,23,33(again)']
2024-03-11 -> ['207(again!!!)', '152,153(again),190,191,198,200,206(again)', '121,124(again),125,128,133(again)', '55(again),56,62,70', '1,3,5,4,19']
2024-03-10 -> ['207(again!!!)', '152,153(again),190,191,198,200,206(again)', '139(again),141,143(again)', '100,102,104,105(again)']
2024-03-12 -> ['207(again!!!),208', '139(again),141,143(again)', '73,76,79(again),91,25', '20,21,23,33(again)']
2024-03-11 -> ['207(again!!!),208', '152,153(again),190,191,198,200,206(again)', '121,124(again),125,128,133(again)', '55(again),56,62,70', '1,3,5,4,19']
2024-03-10 -> ['207(again!!!),208', '152,153(again),190,191,198,200,206(again)', '139(again),141,143(again)', '100,102,104,105(again)']
2024-03-09 -> ['152,153(again),190,191,198,200,206(again)', '139(again),141,143(again)', '121,124(again),125,128,133(again)', '73,76,79(again),91,25']
2024-03-08 -> ['139(again),141,143(again)', '121,124(again),125,128,133(again)', '100,102,104,105(again)', '55(again),56,62,70', '39,48(again),49,53,54']
2024-03-07 -> ['121,124(again),125,128,133(again)', '100,102,104,105(again)', '73,76,79(again),91,25', '20,21,23,33(again)']
Expand Down
162 changes: 67 additions & 95 deletions leetcode_java/src/main/java/LeetCodeJava/Trie/ImplementTrie.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,115 +9,85 @@
* boolean param_2 = obj.search(word);
* boolean param_3 = obj.startsWith(prefix);
*/
import java.util.HashMap;
import java.util.Map;

public class ImplementTrie {


// V0
// IDEA : https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Tree/implement-trie-prefix-tree.py#L49
// TODO : implement above

// V0'
// TODO : fix below

// public class ListNode2{
//
// // attr
// public String val;
// public ListNode2 next;
//
// // constructor
// public ListNode2(){
//
// }
//
// public ListNode2(String val){
// this.val = val;
// }
//
// ListNode2(String val, ListNode2 next){
// this.val = val;
// this.next = next;
// }
//
// }

// class Trie {
//
// // attr
// //LinkedList<String> node;
// ListNode2 node;
// HashMap<String, ListNode2> map;
//
// // constructor
// public Trie() {
// this.node = new ListNode2();
// this.map = new HashMap<>();
// }
//
// public void insert(String word) {
// String _first = String.valueOf(word.charAt(0));
// if (!this.map.containsKey(_first)){
// this.map.put(_first, new ListNode2(_first));
// }else{
// ListNode2 tmpNode = this.map.get(_first);
// ListNode2 tmpNodeUpdated = this.addElementToNode(_first, tmpNode);
// map.put(_first, tmpNodeUpdated);
// }
// }
//
// public boolean search(String word) {
//
// char[] charArray = word.toCharArray();
// String _first = String.valueOf(word.charAt(0));
// if (! startsWith(_first)){
// return false;
// }
// if (!this.map.containsKey(_first)){
// return false;
// }
// ListNode2 curNode = this.map.get(_first);
// while (curNode.next != null){
//
// }
//
// return true;
// }
//
// public boolean startsWith(String prefix) {
//
// return false;
// }
//
// private ListNode2 addElementToNode(String element, ListNode2 node){
//
// return node;
// }
//
// }
// modified by GPT
class TrieNode {
Map<String, TrieNode> children;
boolean isWord;

public TrieNode() {
children = new HashMap<>();
isWord = false;
}
}

class Trie {
TrieNode root;

public Trie() {
root = new TrieNode();
}

public void insert(String word) {
TrieNode cur = root;
for (String c : word.split("")) {
cur.children.putIfAbsent(c, new TrieNode());
cur = cur.children.get(c);
}
cur.isWord = true;
}

public boolean search(String word) {
TrieNode cur = root;
for (String c : word.split("")) {
cur = cur.children.get(c);
if (cur == null) {
return false;
}
}
return cur.isWord;
}

public boolean startsWith(String prefix) {
TrieNode cur = root;
for (String c : prefix.split("")) {
cur = cur.children.get(c);
if (cur == null) {
return false;
}
}
return true;
}
}

// V1
// https://leetcode.com/problems/implement-trie-prefix-tree/editorial/
class TrieNode {
class TrieNode2 {

// R links to node children
private TrieNode[] links;
private TrieNode2[] links;

private final int R = 26;

private boolean isEnd;

public TrieNode() {
links = new TrieNode[R];
public TrieNode2() {
links = new TrieNode2[R];
}

public boolean containsKey(char ch) {
return links[ch -'a'] != null;
}
public TrieNode get(char ch) {
public TrieNode2 get(char ch) {
return links[ch -'a'];
}
public void put(char ch, TrieNode node) {
public void put(char ch, TrieNode2 node) {
links[ch -'a'] = node;
}
public void setEnd() {
Expand All @@ -128,20 +98,21 @@ public boolean isEnd() {
}
}


class Trie2 {
private TrieNode root;
private TrieNode2 root;

public Trie2() {
root = new TrieNode();
root = new TrieNode2();
}

// Inserts a word into the trie.
public void insert(String word) {
TrieNode node = root;
TrieNode2 node = root;
for (int i = 0; i < word.length(); i++) {
char currentChar = word.charAt(i);
if (!node.containsKey(currentChar)) {
node.put(currentChar, new TrieNode());
node.put(currentChar, new TrieNode2());
}
node = node.get(currentChar);
}
Expand All @@ -151,8 +122,8 @@ public void insert(String word) {

// search a prefix or whole key in trie and
// returns the node where search ends
private TrieNode searchPrefix(String word) {
TrieNode node = root;
private TrieNode2 searchPrefix(String word) {
TrieNode2 node = root;
for (int i = 0; i < word.length(); i++) {
char curLetter = word.charAt(i);
if (node.containsKey(curLetter)) {
Expand All @@ -166,19 +137,20 @@ private TrieNode searchPrefix(String word) {

// Returns if the word is in the trie.
public boolean search(String word) {
TrieNode node = searchPrefix(word);
TrieNode2 node = searchPrefix(word);
return node != null && node.isEnd();
}

// Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) {
TrieNode node = searchPrefix(prefix);
TrieNode2 node = searchPrefix(prefix);
return node != null;
}

}


// V2
// https://leetcode.com/problems/implement-trie-prefix-tree/solutions/3309950/java-easiest-solution/
class TrieNode3 {
Expand Down

0 comments on commit e37941d

Please sign in to comment.