Trie数据结构的Strider讲解
class Node{ Node [] node = new Node[26]; boolean flag; public Node(){ } public boolean containsKey(char c){ return node[c-'a']!=null; } public void put(char c, Node n){ node[c-'a'] = n; } public Node get(char c){ return node[c-'a']; } public void setFlag() { this.flag = true; } public boolean getFlag(){ return this.flag; } } class Trie { Node root; public Trie() { root = new Node(); } //will take tc : O(len) of the word public void insert(String word) { Node node = root; for(int i =0;iTrie数据结构二
奋斗者的解释,以便更好地理解
import java.util.* ; import java.io.*; class Node { Node node[] = new Node[26]; int endWith = 0;// will keep track of no. of words ending with this word int countPrefix=0;// will keep track of no. of words starting with this word public Node(){ } public boolean containsKey(char c){ return node[c-'a']!=null; } public void put(char c, Node n){ node[c-'a'] = n; } public Node get(char c){ return node[c-'a']; } public void incrementCountPrefix() { this.countPrefix; } public void decrementCountPrefix(){ --this.countPrefix; } public void incrementEndWith(){ this.endWith; } public void deleteEndWith(){ --this.endWith; } public int getCountPrefix(){ return this.countPrefix; } public int getEndWith(){ return this.endWith; } } public class Trie { Node root; public Trie() { // Write your code here. root = new Node(); } public void insert(String word) { Node node = root; for(int i =0;i完整字符串
// tc : O(n*l) import java.util.* ; import java.io.*; class Node{ Node[] node = new Node[26]; boolean flag; public Node(){} public void put(char c , Node n){ node[c-'a'] = n; } public boolean containsKey(char c){ return node[c-'a']!=null; } public Node get(char c){ return node[c-'a']; } public void setEnd(){ this.flag = true; } public boolean isEnd(){ return this.flag; } } class Trie{ Node root; public Trie(){ root = new Node(); } public boolean checkIfPrefixPresent(String s){ Node node = root; boolean flag= true; for(int i = 0;i计算不同子串的个数
Tc:在
中插入不同的唯一子字符串的 O(n^2) Trie数据结构import java.util.ArrayList; public class Solution { Node root; static int count; public Solution(){ root = new Node(); } public static int countDistinctSubstrings(String s) { count = 0; // Write your code here. Solution sol = new Solution(); for(int i =0;i
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3