Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2020
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. class TrieNode {
  2.     char val;
  3.     boolean end;
  4.     String word;
  5.     Map<Character, TrieNode> children;
  6.    
  7.     public TrieNode(char val, boolean end, String word) {
  8.         this.val = val;
  9.         this.end = end;
  10.         this.word = word;
  11.         this.children = new HashMap<>();
  12.     }
  13.    
  14.     public TrieNode(char val, boolean end) {
  15.         this(val, end, "");
  16.     }
  17.    
  18.     public void add(char val, boolean end, String word) {
  19.         this.children.put(val, new TrieNode(val, end, word));
  20.     }
  21.    
  22.     public boolean contains(char val) {
  23.         return this.children.containsKey(val);
  24.     }
  25.    
  26.     public TrieNode get(char val) {
  27.         return this.children.get(val);
  28.     }
  29.    
  30.     public boolean isEnd() {
  31.         return this.end;
  32.     }
  33. }
  34.  
  35. class Trie {
  36.     TrieNode root;
  37.    
  38.     public Trie() {
  39.         this.root = new TrieNode('!', false);
  40.     }
  41.    
  42.     public void add(String word) {
  43.         TrieNode current = this.root;
  44.         for (int i = 0; i < word.length(); ++i) {
  45.             final char letter = word.charAt(i);
  46.             final boolean end = i == word.length() - 1;
  47.            
  48.             if (current.contains(letter)) {
  49.                 current = current.get(letter);
  50.                 if (end) {
  51.                     current.end = true;
  52.                     current.word = word;
  53.                 }
  54.             } else {
  55.                 current.add(letter, end, word);
  56.                 current = current.get(letter);
  57.             }
  58.         }
  59.     }
  60.    
  61.     public String get(String word) {
  62.         TrieNode current = this.root;
  63.         for (int i = 0; i < word.length(); ++i) {
  64.             final char letter = word.charAt(i);
  65.             final boolean end = i == word.length() - 1;
  66.             if (current.isEnd()) return current.word;
  67.             if (!current.contains(letter)) return "";
  68.             current = current.get(letter);
  69.             if (end && !current.isEnd()) return "";
  70.             if (end) break;
  71.         }
  72.  
  73.         return current.word;
  74.     }
  75. }
  76.  
  77. class Solution {
  78.     public String replaceWords(List<String> dict, String sentence) {
  79.         Trie trie = new Trie();
  80.         List<String> res = new ArrayList<>();
  81.        
  82.         for (String word : dict) {
  83.             trie.add(word);
  84.         }
  85.        
  86.         for (String word : sentence.split(" ")) {
  87.             final String trieWord = trie.get(word);
  88.             if (trieWord == "") {
  89.                 res.add(word);
  90.             } else {
  91.                
  92.                 res.add(trieWord);
  93.             }
  94.         }
  95.        
  96.         return String.join(" ", res.toArray(new String[res.size()]));
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement