Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Trie {
- public Trie[] children;
- public boolean words;
- char characters;
- public String string;
- public Trie() {
- this.children = new Trie[27];
- words = false;
- }
- public void insert(String letters) {
- this.string = letters;
- if(string.isEmpty()){
- this.words = true;
- this.children[26] = new Trie();
- return;
- }
- char string = letters.charAt(0);
- int index = string - 'a';
- if(this.children[index] == null){
- this.children[index] = new Trie();
- this.children[index].characters = string;
- }
- this.children[index].insert(letters.substring(1));
- }
- public boolean query(String string) {
- if(string.isEmpty())
- return this.words;
- int i = string.charAt(0) - 'a';
- if(this.children[i] == null){
- return false;
- }
- return this.children[i].query(string.substring(1));
- }
- public String getString(){
- return string;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement