Class WordFilter

java.lang.Object
g0701_0800.s0745_prefix_and_suffix_search.WordFilter

public class WordFilter extends Object
745 - Prefix and Suffix Search.<p>Hard</p> <p>Design a special dictionary with some words that searchs the words in it by a prefix and a suffix.</p> <p>Implement the <code>WordFilter</code> class:</p> <ul> <li><code>WordFilter(string[] words)</code> Initializes the object with the <code>words</code> in the dictionary.</li> <li><code>f(string prefix, string suffix)</code> Returns <em>the index of the word in the dictionary,</em> which has the prefix <code>prefix</code> and the suffix <code>suffix</code>. If there is more than one valid index, return <strong>the largest</strong> of them. If there is no such word in the dictionary, return <code>-1</code>.</li> </ul> <p><strong>Example 1:</strong></p> <p><strong>Input</strong></p> <p>[&ldquo;WordFilter&rdquo;, &ldquo;f&rdquo;]</p> <p>[<a href="&quot;apple&quot;">&quot;apple&quot;</a>, [&ldquo;a&rdquo;, &ldquo;e&rdquo;]]</p> <p><strong>Output:</strong> [null, 0]</p> <p><strong>Explanation:</strong></p> <pre><code> WordFilter wordFilter = new WordFilter([&quot;apple&quot;]); wordFilter.f(&quot;a&quot;, &quot;e&quot;); // return 0, because the word at index 0 has prefix = &quot;a&quot; and suffix = 'e&quot;. </code></pre> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= words.length <= 15000</code></li> <li><code>1 <= words[i].length <= 10</code></li> <li><code>1 <= prefix.length, suffix.length <= 10</code></li> <li><code>words[i]</code>, <code>prefix</code> and <code>suffix</code> consist of lower-case English letters only.</li> <li>At most <code>15000</code> calls will be made to the function <code>f</code>.</li> </ul>
  • Constructor Details

    • WordFilter

      public WordFilter(String[] words)
  • Method Details

    • insert

      public void insert(String wd, int weight)
    • f

      public int f(String prefix, String suffix)
    • startsWith

      public int startsWith(String pref)