Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. public class Trie {
  2. public Trie[] children;
  3. public boolean words;
  4. char characters;
  5. public String string;
  6.  
  7. public Trie() {
  8. this.children = new Trie[27];
  9. words = false;
  10. }
  11. public void insert(String letters) {
  12. this.string = letters;
  13. if(string.isEmpty()){
  14. this.words = true;
  15. this.children[26] = new Trie();
  16. return;
  17. }
  18. char string = letters.charAt(0);
  19. int index = string - 'a';
  20. if(this.children[index] == null){
  21. this.children[index] = new Trie();
  22. this.children[index].characters = string;
  23. }
  24. this.children[index].insert(letters.substring(1));
  25. }
  26. public boolean query(String string) {
  27. if(string.isEmpty())
  28. return this.words;
  29. int i = string.charAt(0) - 'a';
  30. if(this.children[i] == null){
  31. return false;
  32. }
  33. return this.children[i].query(string.substring(1));
  34. }
  35. public String getString(){
  36. return string;
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement