Does anyone know the fastest way of comparing a single string to n number of strings for a match?
For eg: The word “example” needs to be compared with a list, containing n number of words, for a match. The list may contain any number of words, of any length.
Is there a specific algorithm I can use to do this? I know of string matching algorithms which find a substring within a string, such as the Boyer-Moore Algorithm. But not for this one. Please help me out here. Note that I will be implementing this in Java.
You can prepare a
Map<Int,List<String>>for your list where the key is the.hashcode()for the string and the List contains all strings with the same hashcode.Then you just look up the hashcode for your new string,and run equals() on each string in the returned list.
Should be much faster as there are much fewer entries to compare. Note that the preparation needs some time, so only do this if you need to do it more than once.