|
| 1 | +package design; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +/** |
| 7 | + * Created by gouthamvidyapradhan on 25/12/2017. |
| 8 | + * Given many words, words[i] has weight i. |
| 9 | +
|
| 10 | + Design a class WordFilter that supports one function, WordFilter.f(String prefix, String suffix). It will return the |
| 11 | + word with given prefix and suffix with maximum weight. If no word exists, return -1. |
| 12 | +
|
| 13 | + Examples: |
| 14 | + Input: |
| 15 | + WordFilter(["apple"]) |
| 16 | + WordFilter.f("a", "e") // returns 0 |
| 17 | + WordFilter.f("b", "") // returns -1 |
| 18 | + Note: |
| 19 | + words has length in range [1, 15000]. |
| 20 | + For each test case, up to words.length queries WordFilter.f may be made. |
| 21 | + words[i] has length in range [1, 10]. |
| 22 | + prefix, suffix have lengths in range [0, 10]. |
| 23 | + words[i] and prefix, suffix queries consist of lowercase letters only. |
| 24 | +
|
| 25 | + Solution: Implement a trie to store the dictionary of words. For every word insert all the possible suffixes into |
| 26 | + the trie. Additionally overwrite weight each time a word is inserted. |
| 27 | + Example for a word 'cat' all the possible trie insertions are -> #cat, t#cat, at#cat, cat#cat |
| 28 | + Search for 'suffix#prefix' in the trie and return its weight |
| 29 | + */ |
| 30 | +public class WordFilter { |
| 31 | + |
| 32 | + private Trie trie; |
| 33 | + private int maxWeight; //max weight possible |
| 34 | + /** |
| 35 | + * Main method |
| 36 | + * |
| 37 | + * @param args |
| 38 | + * @throws Exception |
| 39 | + */ |
| 40 | + public static void main(String[] args) throws Exception { |
| 41 | + String[] words = {"apple", "cat", "mat", "mars", "abxcd", "abycd", "apple"}; |
| 42 | + WordFilter wf = new WordFilter(words); |
| 43 | + System.out.println(wf.f("abx", "")); |
| 44 | + } |
| 45 | + |
| 46 | + public WordFilter(String[] words) { |
| 47 | + trie = new Trie(); |
| 48 | + trie.weight = -1; |
| 49 | + maxWeight = words.length - 1; |
| 50 | + for(int i = 0; i < words.length; i ++){ |
| 51 | + String word = words[i]; |
| 52 | + trie.insert("#" + word, i); |
| 53 | + for(int j = 0, l = word.length(); j < l; j++){ |
| 54 | + trie.insert(word.substring(j, l) + "#" + word, i); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public int f(String prefix, String suffix) { |
| 60 | + if((suffix == null || suffix.isEmpty()) && (prefix == null || prefix.isEmpty())){ |
| 61 | + return maxWeight; |
| 62 | + } else if(prefix == null || prefix.isEmpty()){ |
| 63 | + return trie.search(suffix + "#"); |
| 64 | + } else if(suffix == null || suffix.isEmpty()){ |
| 65 | + return trie.search("#" + prefix); |
| 66 | + } else{ |
| 67 | + return trie.search(suffix + "#" + prefix); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public static class Trie { |
| 72 | + |
| 73 | + private Map<Character, Trie> map; |
| 74 | + int weight; |
| 75 | + |
| 76 | + |
| 77 | + /** |
| 78 | + * Initialize your data structure here. |
| 79 | + */ |
| 80 | + public Trie() { |
| 81 | + map = new HashMap<>(); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Inserts a word into the trie. |
| 86 | + */ |
| 87 | + public void insert(String word, int weight) { |
| 88 | + if (word != null) { |
| 89 | + add(0, word, word.length(), weight); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private void add(int i, String word, int length, int weight) { |
| 94 | + if (i < length) { |
| 95 | + char c = word.charAt(i); |
| 96 | + map.putIfAbsent(c, new Trie()); |
| 97 | + Trie subTrie = map.get(c); |
| 98 | + subTrie.weight = weight; |
| 99 | + subTrie.add(i + 1, word, length, weight); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Returns if the word is in the trie. |
| 105 | + */ |
| 106 | + public int search(String word) { |
| 107 | + if (word != null) { |
| 108 | + return search(0, word, word.length()); |
| 109 | + } |
| 110 | + return -1; |
| 111 | + } |
| 112 | + |
| 113 | + private int search(int i, String word, int length) { |
| 114 | + if (i < length) { |
| 115 | + char c = word.charAt(i); |
| 116 | + Trie subTrie = map.get(c); |
| 117 | + if (subTrie == null) |
| 118 | + return -1; |
| 119 | + return subTrie.search(i + 1, word, length); |
| 120 | + } |
| 121 | + return this.weight; |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments