CatLord
CatLord

Reputation: 381

Algorithm to compare one array of strings to many array of strings

I am a bit stuck on this one problem in the current project I am working on:

I want to take one ArrayList<String> (call it A) and compare it to many ArrayList<String>, making note of how many strings match in these array comparisons. Then, I want to order the multiple string arrays in order from most similar to least similar to A.

Does anyone know any fast algorithms to do this? Not looking for code so much as algorithms, but I am working in Java.

Thanks!

Upvotes: 5

Views: 866

Answers (2)

Daniel Karlsson
Daniel Karlsson

Reputation: 1

If you want to compare the similarity (or difference) between strings then try an edit distance algorithm such as the Levenshtein distance algorithm.

http://en.wikipedia.org/w/index.php?title=Levenshtein_distance

Upvotes: 0

Eran
Eran

Reputation: 393846

My suggestion :

  1. First put all the Strings of the reference ArrayList in a Set.
  2. Go over all the members of each of the other ArrayLists, and using set.contains(string) find out how many matches each array list contains.
  3. For each ArrayList, create an object that wraps an ArrayList in addition to the number of matches for that ArrayList.
  4. Finally, sort those wrapper objects according to the number of matches.

Upvotes: 4

Related Questions