Problem solution · Java

Design Search Autocomplete System

Design Search Autocomplete System: a Java solution using sorting and greedy selection. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Sorting and greedy selection
Source
walkccc LeetCode Solutions
Length
74 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Sorting and greedy selection

For Design Search Autocomplete System, the implementation first exposes a useful order, then scans that order while making locally justified choices.

  1. Choose the key that reveals the greedy or grouping structure.
  2. Sort the relevant records by that key.
  3. Scan in order, maintaining the invariant that makes each local choice safe.

Code notes

  • 74 lines of Java from the credited upstream file 642.java.
  • The implementation visibly relies on sequence storage.
  • 4 loop blocks detected.

Complexity

Sorting is typically the dominant term unless the subsequent scan uses a more expensive nested operation.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeDesign Search Autocomplete System · JavaJava
Use this to learn the idea, then write your own version.
class TrieNode implements Comparable<TrieNode> {  public TrieNode[] children = new TrieNode[128];  public String s = null;  public int time = 0;  public List<TrieNode> top3 = new ArrayList<>();   public int compareTo(TrieNode o) {    if (this.time == o.time)      return this.s.compareTo(o.s);    return o.time - this.time;  }   public void update(TrieNode node) {    if (!this.top3.contains(node))      this.top3.add(node);    Collections.sort(top3);    if (top3.size() > 3)      top3.remove(top3.size() - 1);  }} class AutocompleteSystem {  public AutocompleteSystem(String[] sentences, int[] times) {    for (int i = 0; i < sentences.length; ++i)      insert(sentences[i], times[i]);  }   public List<String> input(char c) {    if (c == '#') {      insert(sb.toString(), 1);      curr = root;      sb = new StringBuilder();      return new ArrayList<>();    }     sb.append(c);     if (curr != null)      curr = curr.children[c];    if (curr == null)      return new ArrayList<>();     List<String> ans = new ArrayList<>();     for (TrieNode node : curr.top3)      ans.add(node.s);     return ans;  }   private TrieNode root = new TrieNode();  private TrieNode curr = root;  private StringBuilder sb = new StringBuilder();   private void insert(final String s, int time) {    TrieNode node = root;    for (final char c : s.toCharArray()) {      if (node.children[c] == null)        node.children[c] = new TrieNode();      node = node.children[c];    }    node.s = s;    node.time += time;     // Walk the path again and update the node with leaf node    TrieNode leaf = node;    node = root;    for (final char c : s.toCharArray()) {      node = node.children[c];      node.update(leaf);    }  }} 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗