1

I'm developing a dictionary kind of application using python. In my code, there is a list which consists of sorted set of strings. when a user give some text, I want to get all the string starting with the given string. In other words, I just want to suggest words while user is typing.

Example : If user typed the word "sub", I want to take all the string from the list starting with the substring "sub".

Can anyone give me an algorithm to do this? Thanks all.

3
  • this features is usually referred to as auto-complete; however, if you query an Internet search engine for "python and "auto-complete", most of the results will relate to auto-complete python syntax for text editors. Commented Mar 20, 2012 at 19:45
  • Consider Huffman coding as food for thought on this problem: en.wikipedia.org/wiki/Huffman_coding Commented Mar 20, 2012 at 22:12
  • Possible duplicate: stackoverflow.com/questions/2332028/… Commented Mar 21, 2012 at 0:45

2 Answers 2

1

Depending on the size of the list, you could just iterate through it and use the startswith() string function to get the result. If that's too slow, a common way is to use a prefix tree.

Sign up to request clarification or add additional context in comments.

Comments

0

What you need is a trie data structure, which is perfect for what you seek. Your code needs to handle heavy read/retrieval. Look up trie. if you need implementation let me know.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.