Striver explanation of trie data structure
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 data structure II
striver's explanation for better understanding
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;iComplete String
// 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;iCount distinct substring
Tc: O(n^2) for inserting different unique substring in
Trie data structureimport 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
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3