I have a list of strings. I want to find all of the strings that start or end with another string. At its simplest an example is;
List<string> allWords = new List<string>();
for(int index = 0; index < 1000000; index++)
allWords.Add(index.ToString());
List<string> result = allWords.FindAll(x => x.StartsWith("10") || x.EndsWith("10"));
This algorithm scans the list from beginning to end. I need to perform this operation very quickly and O(n) is too slow.
What data structures (if any) are available to me to solve this algorithm faster that O(n)?
If you have an unsorted
List<string>
, there is no way to do it in less thanO(n)
. However, you could use a different data structure. A trie (also called prefix tree) is particularly well suited for your need, as it has aO(m)
search complexity (wherem
is the length of the searched prefix)I have a C# implementation here : Trie.cs (actually, it's a trie-based dictionary, which associates a value with each key, but for your use-case you can just ignore the value; or if you prefer you can adapt the implementation to your needs).