<p>I'd like to implement a simple class (in Java) that would allow me to register and deregister strings, and on the basis of the current set of strings auto-complete a given string. So, the interface would be:</p>

<ul>
<li>void add(String)</li>
<li>void remove(String)</li>
<li>String complete(String)</li>
</ul>

<p>What's the best way to do this in terms of algorithms and data-structures?</p>

[{'answer_id': 73161, 'author': 'Jon Homan', 'author_id': 7589, 'author_profile': 'https://Stackoverflow.com/users/7589', 'pm_score': -1, 'selected': False, 'text': '<p>Regular expressions.</p>\n'}, {'answer_id': 73345, 'author': 'johnmcase', 'author_id': 12480, 'author_profile': 'https://Stackoverflow.com/users/12480', 'pm_score': 0, 'selected': False, 'text': '<p>It would have to be some kind of a List that you can maintain in sorted order.  You would also have to write your own search algorithm that would give you the index of the first element in the list that matches your search pattern.  Then iterate from that index until the first element that doesn\'t match and you have your list of possible completions.</p>\n\n<p>I\'d look at <a href="http://commons.apache.org/collections/api-release/org/apache/commons/collections/list/TreeList.html" rel="nofollow noreferrer">TreeList</a> from commons-collections.  It has fast insert and remove times from the middle of the List which you\'ll want in order to maintain sorted order.  It would probably be fairly easy to write your search function off of the tree that backs that list.</p>\n'}, {'answer_id': 73358, 'author': 'Aidos', 'author_id': 12040, 'author_profile': 'https://Stackoverflow.com/users/12040', 'pm_score': 2, 'selected': False, 'text': "<p>The datastructure you are after is called a Ternary Search Tree.</p>\n\n<p>There's a great JavaWorld example at www.javaworld.com/javaworld/jw-02-2001/jw-0216-ternary.html</p>\n"}, {'answer_id': 73616, 'author': 'pgras', 'author_id': 12719, 'author_profile': 'https://Stackoverflow.com/users/12719', 'pm_score': 3, 'selected': True, 'text': "<p>you should consider to use a PATRICIA trie for the data structure. Search for 'patricia trie' on google and you'll find a lot of information...</p>\n"}, {'answer_id': 1944664, 'author': 'Shilad Sen', 'author_id': 141245, 'author_profile': 'https://Stackoverflow.com/users/141245', 'pm_score': 0, 'selected': False, 'text': '<p>For those who stumble upon this question...</p>\n\n<p>I just posted a <a href="http://code.google.com/p/autocomplete-server/" rel="nofollow noreferrer">server-side autocomplete implementation</a> on Google Code.  The project includes a java library that can be integrated into existing applications and a standalone HTTP AJAX autocomplete server. </p>\n\n<p>My hope is that enables people to incorporate efficient autocomplete into their applications.  Kick the tires!</p>\n'}, {'answer_id': 2493872, 'author': 'Wellington RIbeiro', 'author_id': 299179, 'author_profile': 'https://Stackoverflow.com/users/299179', 'pm_score': 0, 'selected': False, 'text': '<p>I have created a JQuery plugin called Simple AutoComplete, that allows you to add many autocomplete as you want on the same page, and to add filters with extra param, and execute a callback function to bring other params, like the id of the item.</p>\n\n<p>See it at <a href="http://www.idealmind.com.br/projetos/simple-autocomplete-jquery-plugin/" rel="nofollow noreferrer">http://www.idealmind.com.br/projetos/simple-autocomplete-jquery-plugin/</a></p>\n'}]
['https://Stackoverflow.com/questions/73095', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/12547/']